Memcache invalidate entries according to a pattern?

前端 未结 2 1291
-上瘾入骨i
-上瘾入骨i 2021-02-02 14:27

Is there a way to invalidate entries in memcache according to a wildcard key?

So if I have the following memcache keys:

data/1
data/2
data/3


        
相关标签:
2条回答
  • 2021-02-02 15:02

    memcached does not support namespaced deletes.

    The official wiki has a suggestion on how to work around it:

    0 讨论(0)
  • 2021-02-02 15:05

    The best way is to provide a versioning key when creating your memcache key. We do this by providing a single function/method for creating a key on our system.

    $var1 = 123;
    $var2 = 456;
    $cacheKey = makeKey('monkeyInfo', $var1, $var2, ...);
    

    makeKey() uses the information in the cacheKeyVersions array and returns:

    5:monkeyInfo:123:456
    

    Notice the '5' at the beginning. That comes from a hard-coded array of keyNames => versions. So if we want to invalidate EVERY 'monkeyInfo' cache value in the system we simply have to change that number to 6 in the array. From then on the same call will be looking for

    6:monkeyInfo:123:456
    

    Here is an example of what the key version array might look like. The 'makeKey()' call simply looks into this array to get the version number for any given key.

    $cacheKeyVersions = array(
        'monkeyInfo'   => 5,
        'zebraInfo'    => 2
    );
    

    You could do all sorts of things to make the implementation match your needs, but that's the basic gist of it.

    0 讨论(0)
提交回复
热议问题