macha
macha

Reputation: 7487

How does memcache store data?

I am a newbie to caching and have no idea how data is stored in caching. I have tried to read a few examples online, but everybody is providing code snippets of storing and getting data, rather than explaining how data is cached using memcache. I have read that it stores data in key, value pairs , but I am unable to understand where are those key-value pairs stored?

Also could someone explain why is data going into cache is hashed or encrypted? I am a little confused between serialising data and hashing data.

Upvotes: 19

Views: 20175

Answers (3)

Sean Vieira
Sean Vieira

Reputation: 159905

I'm not deeply familiar with memcached, so take what I have to say with a grain of salt :-)

Memcached is a separate process or set of processes that store a key-value store in-memory so they can be easily accessed later. In a sense, they provide another global scope that can be shared by different aspects of your program, enabling a value to be calculated once, and used in many distinct and separate areas of your program. In another sense, they provide a fast, forgetful database that can be used to store transient data. The data is not stored permanently, but in general it will be stored beyond the life of a particular request (it is possible for Memcached to never store your data, so every read will be a miss, but that's generally an indication that you do not have it set up correctly for your use case).

The data going into cache does not have to be hashed or encrypted (but both things can happen to the data, depending on the caching mechanism.)

Serializing data actually has nothing to do with either concept -- instead, it is the process of changing data from one format (generally one suited for in-memory storage) to another one (generally suitable for storage in a persistent medium.) Another term for this process is marshalling and unmarshalling.

Upvotes: 2

bhamby
bhamby

Reputation: 15450

A couple of quotes from the Memcache page on Wikipedia:

Memcached's APIs provide a giant hash table distributed across multiple machines. When the table is full, subsequent inserts cause older data to be purged in least recently used (LRU) order.

And

The servers keep the values in RAM; if a server runs out of RAM, it discards the oldest values. Therefore, clients must treat Memcached as a transitory cache; they cannot assume that data stored in Memcached is still there when they need it.

The rest of the page on Wikipedia is pretty informative, and it might help you get started.

Upvotes: 23

OneOfOne
OneOfOne

Reputation: 99234

They are stored in memory on the server, that way if you use the same key/value often and you know they won't change for a while you can store them in memory for faster access.

Upvotes: 2

Related Questions