Why memcached?

I am trying to figure out what a solution with memcached will need. This may seem like a silly question, but what does it bring to the table if I just need to cache the objects - isn’t it just hashmap?

+6
hashmap memcached
source share
5 answers

Quote from memcache website , memcache ...

Free and open source, high-performance, distributed memory caching system, common in nature, but designed to accelerate dynamic web applications making it easy to load the database.

Memcached is a key-value in memory to store small pieces of arbitrary data (rows, objects) from the results of database calls, API calls, or page rendering. Memcached is simply powerful. Its simple design facilitates rapid deployment, ease of development, and solves many of the problems facing large data caches. Its API is available for the most popular languages.

At heart, this is a simple key / value store

The keyword is distributed here. In general, quoting the memcache site again,

Memcached servers are usually unaware of each other. There is no crosstalk, no synchronization, no broadcasting. Lack of connectivity means adding additional servers will usually add more capacity as you expect. There may be exceptions to this rule, but they are exceptions and are carefully considered.

I highly recommend reading the detailed memcache description .

+3
source share

Where are you going to put this hash? This is what he does for you. Any structure that you implement in PHP exists only until the request is completed. If you throw stuff into a persistent cache, you can retrieve it for other queries instead of rebuilding the data.

+2
source share

I know this question is quite old, but besides the possibility of sharing the cache on several servers, there is another aspect that is not mentioned in other answers, and this is expiration.

If you store values ​​in a HashMap and that the HashMap is tied to the application context, it will continue to grow in size if you do not expire the elements to some extent. Memcached expects an object to achieve maximum performance.

When an item is added to memcache, it can have an expiration time, such as 600 seconds. After the object has expired, it will remain there, but if another object requests it, it will clear it and return null.

Similarly, when memcached memory is full, it will look for the first expired item of sufficient size and expire to make room for the new item. Finally, it can also happen that the cache is full and does not expire, in which case it will replace the least used items.

+2
source share

Using a fully controlled caching system usually allows you to replicate the cache on many servers or simply scale to many servers to scale many concurrent requests, all of which remain acceptable quickly in response.

+1
source share

There is an (old) article comparing various 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 that you will have better results using the cache file system.

Here are the test results for 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
source share

All Articles