开发者

why memcached instead of hashmap

I am trying to understand what would be the need to go with a solution like memcached. It may seem like a silly question - but what does it开发者_JS百科 bring to the table if all I need is to cache objects? Won't a simple hashmap do ?


Quoting from the memcache web site, memcache is…

Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.

At heart it is a simple Key/Value store

A key word here is distributed. In general, quoting from the memcache site again,

Memcached servers are generally unaware of each other. There is no crosstalk, no syncronization, no broadcasting. The lack of interconnections means adding more servers will usually add more capacity as you expect. There might be exceptions to this rule, but they are exceptions and carefully regarded.

I would highly recommend reading the detailed description of memcache.


Where are you going to put this hashmap? That's what it's doing for you. Any structure you implement on PHP is only there until the request ends. If you throw stuff in a persistent cache, you can fetch it back out for other requests, instead of rebuilding the data.


I know that this question is rather old, but in addition to being able to share a cache across multiple servers, there is also another aspect that is not mentioned in other answers and is the values expiration.

If you store the values in a HashMap, and that HashMap is bound to the Application context, it will keep growing in size, unless you expire items in some ways. Memcached expires object lazily for maximum performance.

When an item is added to the memcache, it can have an expiration time, for instance 600 seconds. After the object is expired it will just remain there, but if another object asks for it, it will purge it and return null.

Similarly, when memcached memory is full, it will look for the first expired item of adequate size and expire it to make room for the new item. Lastly, it can also happen that the cache is full and there isn't any item to expire, in which case it will replace the least used items.


Using a fully flagded cache system usually allow you to replicate the cache on many servers, or just scale to many server just to scale a lot of parallel requestes, all this remaining acceptable fast in term of reply.


There is an (old) article that compares different caching systems used by php: https://www.percona.com/blog/2006/08/09/cache-performance-comparison/

Basically, file caching is faster than memcached.

So to answer the question, I believe you would have better performances using a file based cache system.

Here are the results from the tests of the article:

Cache Type                              Cache Gets/sec
Array Cache                             365000
APC Cache                               98000
File Cache                              27000
Memcached Cache (TCP/IP)                12200
MySQL Query Cache (TCP/IP)              9900
MySQL Query Cache (Unix Socket)         13500
Selecting from table (TCP/IP)           5100
Selecting from table (Unix Socket)      7400 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜