Memcache 替代品,更多控制

问题描述 投票:0回答:6

我的新 PHP 应用程序可以通过对 MySQL 结果进行一些缓存来加速。 我对 memcached 的经验有限,但我认为它不能满足我的要求。

当我正在开发多用户应用程序时,我希望能够一次删除多个存储的值,而不删除所有内容

所以我可能会存储:

account_1.value_a = foo
account_1.value_b = bar
account_2.value_a = dog
account_2.value_b = cat

是否有一个缓存系统允许我基于通配符(或类似的方法)进行删除,例如“删除 account_1.*”,留下:

account_1.value_a = <unset>
account_1.value_b = <unset>
account_2.value_a = dog
account_2.value_b = cat

谢谢, 吉姆

php caching memcached
6个回答
5
投票

并非如此,但您可以通过在密钥中使用版本号来伪造它。

例如,如果您使用这样的键:

{entitykey}.{version}.{fieldname}

所以现在你的

account_1
对象键将是:

account_1.1.value_a
account_1.1.value_b

当您想从缓存中删除

account_1
时,只需增加该对象的版本号即可。现在你的钥匙将是:

account_1.2.value_a
account_1.2.value_b

您甚至不需要删除原始缓存值 - 它们会自动从缓存中删除,因为您将不再使用它们。


0
投票

这可能会有所帮助:内存缓存和通配符


0
投票

开源模块,用于获取内存缓存中键的标签,以及其他: http://github.com/jamm/memory/


0
投票

Scache (http://scache.nanona.fi) 具有嵌套键空间,因此您可以在子键上存储数据并在需要时使父键过期。


0
投票

Memcached 按标签删除可以这样完成;

搜索和删除 100,000 个键相当快,但应该在更大的缓存中监控性能。

Php 8.0 之前

$tag = "account_1";
$cached_keys = $this->memcached->getAllKeys();

foreach($cached_keys as $key){
    if(substr($key, 0, strlen($tag)) === $tag){
        $this->memcached->delete($key);
    }
}

PHP 8.0 >

$tag = "account_1";
$cached_keys = $this->memcached->getAllKeys();

foreach($cached_keys as $key){
    if (str_starts_with($key, $tag)) {
        $this->memcached->delete($key);
    }
}

0
投票

可能已经晚了,但是对于任何有同样问题的人,您可以使用APCu,它非常快且轻量级。它可以做OP要求的事情(通过使用正则表达式)。

示例:

apcu_store('user1Address', "some data");
apcu_store('user1Name' , "some other data");
apcu_store('user2Name' , "some other data again");;

echo "All cached data:<br>";
foreach (new APCUIterator('/^.*/') as $item) {
    echo $item['key'] . " -> " . $item['value'] . "<br>";
}


//Delete all cache that thair key start with 'user1'
foreach (new APCUIterator('/^user1.*/') as $item) {
    apcu_delete($item['key']);
}

echo "All cached data after delete:<br>";
foreach (new APCUIterator('/^.*/') as $item) {
    echo $item['key'] . " -> " . $item['value'] . "<br>";
}

输出将是:

All cached data:
user1Name -> some other data
user1Address -> some data
user2Name -> some other data again
All cached data after delete:
user2Name -> some other data again

请注意,默认情况下不安装

APCu

© www.soinside.com 2019 - 2024. All rights reserved.