Cache over 1mb memcache alternatives

余生颓废 提交于 2019-12-29 08:06:06

问题


I have an object in php that is more than one mb. I am using memcache which allows me to store 1mb or data. Does anyone know any other alternatives for data over 1mb. I have read that altering memcache to store more is not the best option.


回答1:


You can use MultipartCache which extends memcache to support data higher than 1MB limit. Please note that in memcached 1.4.2 and higher you can configure the maximum supported object size by using the -I command-line option.

 memcached -I 5m     //default: 1mb, min: 1k, max: 128m

MultipartCache simply splits the data into different parts based on the limit set max 1MB

Example:

$largeSet = range(0, 100000);
$key = "largeSet";

$cache = new MultipartCache();
$cache->setLimit(1024);
$cache->set($key, $largeSet);

The system would split the data base on size/limit which is about 575 different places for the sample above.

To get this information is pretty easy

$dataFromCache = $cache->get($key);

How are we sure that the the data is ok ? that is what the hash is for but lets do a random test

for($i = 0; $i < 20; $i ++) {
    $rand = mt_rand(0, 100000);
    printf("%s - %s\n", $dataFromCache[$rand], assert($dataFromCache[$rand] == $rand) ? "true" : "false");
}

Output

39603 - true
16034 - true
23116 - true
94038 - true
64481 - true
84987 - true
53912 - true
32153 - true
43965 - true
71144 - true
97309 - true
53227 - true
28525 - true
9936 - true
16921 - true
27323 - true
35129 - true
46235 - true
5641 - true
43425 - true

Now lets look at more sensitive information .. like an image ...

$key = "largeImage";
$cache = new MultipartCache();
$cache->addserver("127.0.0.1");
$cache->set($key, file_get_contents("large_image.jpg"));

header("Content-Type: image/jpeg");
echo $cache->get($key);


来源:https://stackoverflow.com/questions/15977676/cache-over-1mb-memcache-alternatives

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!