2

In my website, users will be redirected to a "frontpage" page where they will be able to see the latest news, their special announcements, their own materials, etc. Now I have to use DB to retrieve all those information. I also want to use caching for this purpose so the data on their frontpage refresh every 3 hours let's say instead of each visit.

I am using the following function to achieve this:

function mymodule_cache($reset = FALSE) {
  static $my_data;
  global $user;
  if (!isset($my_data) || $reset) {
    if (!$reset && ($cache = cache_get('user_data:' . $user->uid)) && !empty($cache->data)) {
      $my_data = unserialize($cache->data);
    }
    else {
      $my_data = ... (This is where I do all the DB queries, etc.);
      cache_set('user_data:' . $user->uid, serialize($my_data), 'cache', time() + 10800); 
    }
  }
  return $my_data;

}

now this works great, but my question is how if I have tons of users, let's say 10,000 users, would it be efficient to still using this caching method since we are filling up the table rows with data?

Or if not, what is the best solution?

Thanks Luca

1

In general, yes, you can do this. There is no limitation on the number of cache entries that can be stored. However, if using the default database cache backend, it can fill up your database and when additionally using the default cache bin, it can slow down other cache lookups. So, I would recommend:

Two additional tipps:

  • Monitor the number reads/writes of that cache to see if the cache actually helps. If, say, 50% of the cache entries are only written and never read, you might want to consider to avoid caching this information.

  • Especially when using a cache backend like Memcache (which has a smaller overhead for cache calls), it might be helpful to split the data up into several separate cache entries. There a number of benefits for this:

    1. You can cache certain information longer than others (maybe there only is a new announcement every few days but multiple new comments/whatever per hour
    2. Some of the data might not even be user specific but only specific for the roles a user has or even global. So for example, all anouncements could be read from a single cache entry.
    3. You can use cache_clear_all() to clear specific caches on defined events. For example, clear all news caches if a new news is published. By doing this, all users will be able to see the news immediatly. If this is working well, you can increase the cache expiration time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.