1

I am trying to set cache with in my custom created cache table which is created with below code:

function custom_activity_dashboard_schema() {
  $schema = array();
  $schema['activity_dashboard_cache'] = drupal_get_schema_unprocessed('system', 'cache');
  return $schema;
}

function custom_activity_dashboard_update_7021(){
  $schema = custom_activity_dashboard_schema();
  if(!db_table_exists('activity_dashboard_cache')) {
    db_create_table('activity_dashboard_cache', $schema['activity_dashboard_cache']);
  }
}

Table created looks like screenshot :

enter image description here

Just added simplly this code to test if cache entry get created or not :

   $cache_time = '+10 minutes';
   $expire = strtotime($cache_time);

  $t_revisions = total_revisions();

  // set cache 
  cache_set("total_revisions", $t_revisions, 'activity_dashboard_cache', $expire);

  // get and print cache value
  $cache = cache_get("total_revisions",'activity_dashboard_cache');
  echo $cache->data;

with above code there is no entry in database table when I run query to see :

enter image description here

But when I print value by cache get it prints value that was cached on my page.

I am not able to figure out what I am doing wrong and why cacnot I see entires in database table but the value after cache get is printed.

Please suggest what I am doing worng.

With same code I can see entires in database table on my loacl environment.

UPDATE :

We are using redis and entity_cache for full site.

Thanks!

1

If you are using redis, wouldn't your cache bins not be controlled in the Drupal database anymore? You would therefore need to explicitly set the cache bin to use DrupalDatabaseCache so that it uses the Drupal database in settings.php.

Example:

$conf['cache_class_activity_dashboard_cache'] = 'DrupalDatabaseCache';

This is how it used to be done with Memcache... Redis should be the same, but check the README:

http://cgit.drupalcode.org/redis/tree/README.txt?h=7.x-3.x

It would explain why you can query this on your local machine, if you only have Redis configuration set for the remote environments running it. You are querying the database, but the cache is pulling from Redis.

This is for Memcache, but sheds more light on excluding certain cache, like cache_form - which remains in the database. https://www.drupal.org/node/1131468

  • Thank you very much , I completely missed that I have uninstalled redis on local environment. That will be very helpful if you can help me to clear my concept here that why values print when I use $cache = cache_get("total_revisions",'activity_dashboard_cache'); echo $cache->data; – jas Dec 14 '16 at 19:01
  • Right, the cache API is pulling from the cache configuration. On remote environments it will pull from Redis, locally, it will pull from MySQL. That's what I am saying. The API will work correctly - thats the reason a MYSQL query returns nothing in the database, but cache_get returns a value. Its not in the same store. The settings.php configuration sets the cache backends - if nothing is set, the default is the local database (normal caching). This is why you see it 'working' locally. Cache data is routed to Redis on the remote environment, and not MySQL. – Kevin Dec 14 '16 at 19:03
  • Thanks, I got your point that its in Redis and not in MYSQL. So is that not option to keep using it in same way as its pulling in right way or that will be better to exclude my activity_dashboard_cache from Redis as you suggested ? – jas Dec 14 '16 at 19:08
  • It depends on the data being cached, really. If you need it to be reliable and always fresh, use database cache. However, using the Cache API will always return the right data. Is there a reason you need to query cache tables directly in MySQL? – Kevin Dec 14 '16 at 19:12
  • Nothing but I was just done query so that I can see if cache was set or not. Just for testing ...to make sure otherwise I do not need to run query. But as you said database cache will be more reliable I think I will go with your suggested code above to try. – jas Dec 14 '16 at 19:14

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.