phpcachingsymfony1propel

Caching Symfony custom model class method


I have created a method that takes in a string value, matches it with the user records and returns the matching user's record.

My code:

class UsersPeer extends BaseUsersPeer
{
  public static function getHashUser($hash)
  {
    $c = new Criteria();
    $c->add(UsersPeer::HASH,$hash);
    return UsersPeer::doSelectOne($c);
  }
}

Does it make sense to cache this function?

My understanding says that since the string value passed to the function shall always be different for different users, the function shall execute each time. However, it shall not execute again for the users, for whom the string value has previously been passed to the method and record retrieved. Only in such cases the function caching shall work.

Given that there are approximately 50K users, does it make any sense enabling caching for this method ?


Solution

  • This is a very simple query that should be fast even if the hash column is not indexed (and very fast if it is) - I see no reason to cache the results.

    Result cache is often used in situations where the query is complex, contains aggregate data or subqueries. Even then you should work on the query first to see if you can optimize it.

    Update:

    If you check out the sfCache class (the base for all concrete cache implementations) you'll see that anything you store in a cache has a lifetime, meaning it gets stored only for a limited time. This means the size of the cache is limited.

    Cache works like this:

    Whether it's worth caching a function's return value is always up to performace measurement. Is it a long-running, computation-intensive function? Is it a performance bottleneck? If your answers are positive, it's probably worth enabling caching.

    Also remember, that every time you do function caching it increases the amount (and complexity) of code you have to maintain.