Cache over 1mb memcache alternatives

2019-01-26 18:54发布

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.

标签: php caching
1条回答
啃猪蹄的小仙女
2楼-- · 2019-01-26 19:56

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);
查看更多
登录 后发表回答