本文介绍了缓存超过1mb的内存缓存替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php中有一个超过1 mb的对象.我正在使用memcache,它可以存储1mb或数据.有谁知道超过1mb的数据还有其他选择吗?我已经读过,更改内存缓存以存储更多内容不是最佳选择.

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.

推荐答案

您可以使用 MultipartCache memcache支持高于1MB limit的数据.请注意,在memcached 1.4.2及更高版本中,您可以使用-I命令行选项来配置支持的最大对象大小.

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 只是根据限制集max 1MB

示例:

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

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

系统将在size/limit上拆分数据库,该数据库在上述示例的575个不同位置.

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

获取此信息非常简单

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

我们如何确定数据正常?这就是hash的用途,但让我们进行随机测试

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");
}

输出

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);

这篇关于缓存超过1mb的内存缓存替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 07:27