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.
Asked
Active
Viewed 4,172 times
3
-
The options depend on what you want to store, why you want to store it in memory, how often you need to write, how often you need to read, ... – Tchoupi Apr 12 '13 at 17:46
-
Just curios why you have 1MB Object ? They are creative ways to split it into parts – Baba Apr 12 '13 at 17:52
-
i have 20k records for images on paging. Rather than query the database each time the site loads – Matthew Chambers Apr 12 '13 at 18:02
-
I think i can come up with a work around .. one moment – Baba Apr 12 '13 at 18:07
-
ok thanks let me know most appreciated – Matthew Chambers Apr 12 '13 at 18:18
1 Answers
5
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);

Baba
- 94,024
- 28
- 166
- 217
-
I would keep on improving it ... you can also add [Add Issues](https://github.com/olekukonko/MultipartCache/issues) if any ... – Baba Apr 13 '13 at 16:40