phpzend-frameworkcachingzend-cache

How to use Zend_Cache Identifier?


I think I'm getting crazy, I'm trying to implement Zend_Cache to cache my database query. I know how it works and how to configure.

But I can't find a good way to set the Identifier for the cache entries. I have a method which searches for records in my database (based on an array with search values).

/**
 * Find Record(s)
 * Returns one record, or array with objects
 *
 * @param array   $search Search columns => value
 * @param integer $limit  Limit results
 * @return array One record , or array with objects
 */
public function find(array $search, $limit = null)
{
    $identifier = 'NoIdea';

    if (!($data = $this->_cache->load($identifier))) {
        // fetch
        // save to cache with $identifier..
    }

But what kind of identifier can use in this situation?


Solution

  • The identifier should be a unique string to represent the cache entry.

    You might like to name them based on your database tables and fields, e.g. db_tablename_primarykey_1

    If you have a method with several arguments, it'll be a pain to make a string containing all of them. Alternatively you can concat a function's arguments and the db_tablename to produce a string for hashing. For example:

    $identifier = md5('db_tablename_find' . serialize(func_get_args()));