Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

php - Cache over 1mb memcache alternatives

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
", $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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...