Home  >  Article  >  Backend Development  >  Tips for memcache to cache content?

Tips for memcache to cache content?

WBOY
WBOYOriginal
2016-08-04 09:20:01871browse

I encountered a problem during development today. The system operates frequently on users and user groups, so I plan to store these two in memcache. My initial idea was to find it out from the database and put it directly into a key. (For example, if I check the user group information, I put all the user group information in user_group).
A problem I have now is that if I want to add a new user group, I have to delete the original key, read the database once, and then put it in the cache of user_group. The same goes for deletion. The same goes for modifications.

Then the question is, is there any way to save a specific user group? For example, I use id as the identifier, user_group1 is the cache of administrators, and user_group2 is the cache of ordinary users. Wouldn't this be better? If the cache is really put in this way, then when reading the data (that is, when the content is read out from the database at the beginning), how can it be written to memcache one by one?

Thank you everyone^^

Reply content:

I encountered a problem during development today. The system operates users and user groups frequently, so I plan to store these two in memcache. My initial idea was to find it out from the database and put it directly into a key. (For example, if I check the user group information, I put all the user group information in user_group).
A problem I have now is that if I want to add a new user group, I have to delete the original key, read the database once, and then put it in the cache of user_group. The same goes for deletion. The same goes for modifications.

Then the question is, is there any way to save a specific user group? For example, I use id as the identifier, user_group1 is the cache of administrators, and user_group2 is the cache of ordinary users. Wouldn't this be better? If the cache is really put in this way, then when reading the data (that is, when the content is read out from the database at the beginning), how can it be written to memcache one by one?

Thank you everyone^^

Your ideas are feasible and your worries are unnecessary.
Because the memcached driver of php will automatically serialize and unserialize the data, you don’t have to worry about it at all.
Example

<code>// 0. 准备:
$cache = new Memcache();
$cache->connect('127.0.0.1', 11211);

// 1. 构造cache KEY
$cacheKey = 'user_group_admin';

// 2. 从缓存中获取数据
$data = $cache->get($cacheKey);
if ($data === false|| isChanged()){ 
    // 3. 缓存失效或者有变动,加载数据
    $userList = GetUserListByGroup('Admin');
    // 4. 保存数据到缓存中
    $cache->set($cacheKey, $userList);    
}

// 普通用户组类似
</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn