douciwang6819 2011-12-08 20:05
浏览 55
已采纳

PHP Memcached密钥和增加版本号以使缓存无效 - 如何?

I am beginning to add a memcached layer to my application. so far so good.

I realised quite quickly however that I will be needing to invalidate/delete large batches of keys in one go when someone uploads a file to the site in order to keep the data relevant.

I have done some reading and the most common way of getting around this problem seems to be setting a version number on each key, then rather than deleting the keys when a user uploads (because there could be so many permutations) you incrememnt the version number, inducing a cache miss next time the data is accessed.

I have no idea where to begin in order to get this coded and i'm not quite sure I totally get my head around it anyway.

My code currently looks like this:-

$memcache = new Memcache;


$memcache->connect('localhost', 11211) or die ("Could not connect");


$key = md5("songsearch_$textSearch.$rangeSearch");

The two variables in the key var above are retrieved from the get request which in turn retrieves a large amount of JSON. (Think a product catalogue).

These variables also determine the query itself which is dynamically put together dependant upon these variables. Ultimately all of this gives me a key that is unique to every individual query, even though from the same script I could have hundreds of keys being generated.

I hope the above is clear, if not please ask me to clarify any points in order to better help you answer my question.

Obviously later, to set the cache the results I am using this:-

$memcache->set($key, $output, MEMCACHE_COMPRESSED, time() + 24*60*60*365);

just before I encode the JSON.

So my question really is... How can I add some form of incremental versioning to this so that if a user uploads a file I can invalidate the whole lot of keys that have been generated by this script?

Huge thanks to anyone who gets me at least on the right track.

  • 写回答

2条回答 默认 最新

  • dtxpz8785 2011-12-08 23:58
    关注

    You are obviously on the right track. The only thing you're missing: Store a version number in the memcache, which you retrieve before you build your key.

    $memcache = new Memcache;
    $memcache->connect('localhost', 11211) or die ("Could not connect");
    
    // fetch the current version number
    $version = $memcache->get('version_number');
    
    // and add the version to the key
    $key = md5("songsearch_$textSearch.$rangeSearch") . 'v' . $version;
    

    So now, whenever somebody uploads new content, you simply increment the version number:

    $memcache->increment('version_number');
    

    This automatically leads to all existing queries to run into invalid entries.

    To simplify the access, I recommend you wrap this in a new Memcache handler class (untested):

    class VersionedMemcache extends Memcache {
    
        const VERSION_KEY = 'version_number';
    
        private $version = 1;
    
        public function __construct() {
            $version = parent :: get(self :: VERSION_KEY);
            if ($version === false) {
                $version = 1;
                $this->set(self :: VERSION_KEY, $version);
            }
            $this->version = $version;
        }
    
        public function get($key, &$flags = null) {
            $key = $key . '_v' . $this->version;
            return parent :: get($key, $flags);
        }
    
        public function incVersion() {
            $this->version = $this->increment(self :: VERSION_KEY);
            return $this->version;
        }
    
    }
    

    So now, you would simply amend your script to:

    $memcache = new VersionedMemcache();
    
    $memcache->connect('localhost', 11211) or die ("Could not connect");
    
    $key = md5("songsearch_$textSearch.$rangeSearch");
    
    // this will now result in a fetch on 'ab23...232afe_v1' instead of 'ab23...232afe'
    $result = $memcache->get($key);
    
    // when an upload happens, simply
    $memcache->incVersion();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 Python如何后台操作Vmwake虚拟机键鼠
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容