PHP memcache design patterns

前端 未结 5 1603
天涯浪人
天涯浪人 2020-12-23 12:49

We use memcache basically as an after thought to just cache query results.

Invalidation is a nightmare due to the way it was implemented. We since learned some tech

5条回答
  •  不知归路
    2020-12-23 13:28

    I use the Zend Cache component (you don't have to use the entire framework just the zend cache stuff if you want). It abstracts some of the caching stuff (it supports grouping cache by 'tags' though that feature is not supported for the memcache back end I've rolled my own support for 'tags' with relative ease). So the pattern i use for functions that access cache (generally in my model) is:

    public function getBySlug($ignoreCache = true)
    {
        if($ignoreCache || !$result = $this->cache->load('someKeyBasedOnQuery'))
        {
            $select = $this->select()
                    ->where('slug = ?', $slug);
            $result = $this->fetchRow($select);
    
            try
            {
                $this->cache->save($result,'someKeyBasedOnQuery');
            }
            catch(Zend_Exception $error)
            {
              //log exception
            }
        }
        else
        {
            $this->registry->logger->info('someKeyBasedOnQuery came from cache');
        }
        return $result;
    
    }
    

    basing the cache key on a hash of the query means that if another developer bypasses my models or used another function elsewhere that does the same thing it's still pulled from cache. Generally I tag the cache with a couple generate tag (the name of the table is one and the other is the name of the function). So by default our code invalidates on insert,delete and update the cached items with the tag of the table. All in all caching is pretty automatic in our base code and developers can be secure that caching 'just works' in projects that we do. (also the great side effect of making use of tagging is that we have a page that offers granular cache clearing/management, with options to clear cache by model functions, or tables).

提交回复
热议问题