When used in the project to the cache, we choose Redis or Memcached, Why?

For some scenarios:

A, such as to achieve a simple log collection function or sending a large number of messages, e-mail functions, implementation is first collected in the data queue, and to consume a regular job queue, the process to do something.

Redis direct use of lpush, rpop or rpush, lpop.

//进队列
$redis->lpush(key, value);

//出队列
$redis->rpop(key);

Memcached no such data structure.

Second, we want to store user information such as, ID, name, phone number, age, height, how to store?

A program: key => value

key = user_data_ user ID

value = json_encode (user data)

Query, first remove the key, and then json_decode resolution.

Option Two: hash

key = user_data_ user ID

hashKey = name, value = xx

hashKey = phone, value = xx

hashKey = the age, value = xx

hashKey = Height, value = xx

When a query, you can remove the key.

//新增
$redis->hSet(key, hashKey, value);
$redis->hSet(key, hashKey, value);
$redis->hSet(key, hashKey, value);

//编辑
$redis->hSet(key, hashKey, value);

//查询
$redis->hGetAll(key); //查询所有属性
$redis->hGet(key, hashKey); //查询某个属性

Scheme I Scheme II is superior.

Third, such as social projects like Sina Weibo, a fan and a list of personal watchlist center, two-way Watchlist, as well as the popular microblogging, as well as subscribe to news and so on.

More associated data structures are used to provide Redis.

Four, Memcached is only stored in memory, and Redis may be stored in memory, you can also persisted to disk.

If the data needs of needs persist, please choose Redis.

Personal Memcached is not used in the work, get better when Memcached Redis memory allocation by querying data.

Slab Allocation Memcached default memory management mechanism in accordance with a predetermined size, the allocated memory is divided into blocks corresponding to the length of the key-value store particular data record length, in order to completely solve the problem of memory fragmentation.

How to ensure data consistency and cache database?

New data: first added to the database, and then added to the cache.

Edit Data: delete the cached data, modify data in the database, and then added to the cache.

Delete the data: delete the cache data, delete data in the database.

Query data: first data query cache, no, and then query the database, and then add to the cache.

Strong consistency is difficult to guarantee, such as transactional consistency, consistency point in time, the final consistency.

Guess you like

Origin www.cnblogs.com/flzs/p/12205733.html