开发者

Should a Cache object read directly from filesystem?

I'm building a server that implement the tftp protocol. My homework requests the creation of a cache of recent requested files. What I would like to understand is about design, watch the example:

Server request file to the cache The cache doesn't have file The cache read from fi开发者_如何转开发lesystem the file The cache serves the file to the server

Should the cache read from the file system or should the server read from the cache and if the file is missing, read it from the file system and put it in the cache?


From a complexity standpoint, I would definitely recommend that the cache reads the file from the file system. Your implementation will be much cleaner that way.

Digging deeper: you're touching on the single responsibility principle. Ideally, you want components of your system to do one thing, and do it well. What you are trying to avoid is a God object that does everything, as this stops your code from being scalable and reusable. Now lets take a look at the two options you presented:


Option 1: Server reads file system & saves to cache.

In this instance, the server is the center of the universe. The cache serves as nothing more than a memory pool for the server, and without the server, it has little purpose. The server must know everything about both the file system and the cache, and enhancing the system requires that the server be changed.

This description alone makes it obvious why it breaks the single responsibility principle. Any change to any component of the system demands a change to the server--this is bad.


Option 2: Cache reads file system.

In this option, the cache serves as a complete abstraction between the server and the file system. The server does not need to know how the cache works. For that matter, there could even be multiple levels of caching! No matter how it works, the server doesn't have to care. The server uses the cache for one use and that is to retrieve a file.

The division also goes both ways. The cache does not need not know how it is being used, but merely that it exists to make accessing the file system faster. This allows it to be switched out with something better at a later date (if you decide to do so), and also makes it so that it can be reused in other projects you create.

Your code will be much easier and much cleaner if you go with option 2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜