1

My requirement is to update some params in the script for a some specific nodes. If a set of nodes is attached to an X content type, it should take some params in a script. For this purpose I am running a cron job every 10 min as new contents can be attached to that X content type. In this cron job, I am fetching all nodes belonging to X content type and also from these nodes, I am getting set of nodes attached to each X node. I am storing the corresponding param value for a node in the cache using the function cache_set().

cache_set(node id, X node id, MY_CACHE_NAME, CACHE_PERMANENT)

But the problem is that some nodes are not taking the param sometimes. I think that it is setting the cache, but in the cache table something wrong is happening. But when I run 3 or 4 times the nodes which were not taking param started taking. I am not getting what is happening that cache table.

I am not getting where it is writing to the database. How can we test the problems related to these caching?

I am clearing the corresponding MYCACHE before every cron runs.

hook_cron {
  cache_clear_all('*', MY_CACHE, TRUE);
  take_params();
}

What am I doing wrong?

3
  • cron only runs a certain number of items at a time, otherwise users could be stuck waiting for it. Have you tried or considered using Rules? having it run on new content or update of content? then you don't need to worry about cron at all - it would occur immediately.
    – Geoff
    Commented Jun 19, 2014 at 13:44
  • Hi @Geoff, it won't do anything to the content.It will take node ids and use it as a cache id and set the value to the X content type node id (as i mentioned in the question) .Can you please tell me what is actually happening in cache_set() and where it stores my data?
    – Vishnu
    Commented Jun 19, 2014 at 14:39
  • Good guide lullabot.com/blog/article/beginners-guide-caching-data-drupal-7
    – mikeytown2
    Commented Jun 19, 2014 at 22:06

1 Answer 1

0

Your use of the functions cache_set() and cache_clear_all() does not look right.

First let us look at cache_set(). It stores some serialized data in some persistent cache bin.

Its arguments are:

  • $cid - a string that identifies the stored data. The same string is stored in the $cid field in the database table named by the $bin argument.
  • $data - the data to cache. This is stored in the data field identified by $bin and $cid, usually as a serialized object.
  • $bin - The name (a string) of the cache bin to use. This corresponds to a database table with that name. It should either be 'cache' or start with the string 'cache_'.
  • $expire - when should the item expire. If set to CACHE_PERMANENT, the item can only be expired by calling cache_clear_all().

You want to cache some parameters attached to some node. To do that, you may use:

$cid = 'mymodule:node_' . $nid;
$data = ...; // Set it to hold whatever object you want to cache.
$bin = 'cache';
cache_set($cid, $data, $bin, CACHE_PERMANENT);

Note that $bin need to be set to the name of one of the bins (i.e. cache database tables) that are already defined.

Now, to clear out this particular item, you need to call cache_clear_all() with the same $cid and $bin as was you to set the cache. The final parameter to cache_clear_all() can indicate that $cid is a wildcard (in your case, it is not). I.e.:

cache_clear_all($cid, $bin, FALSE);
5
  • Hi @Free Radical ,Sorry for the incompletion of the function.I have written the cron as you mentioned.I believe that problem is with cache_set() function.Can you tell me what is actually happening in cache_set() ? I am not aware of the third parameter in cache_set ,is it a table?
    – Vishnu
    Commented Jun 19, 2014 at 14:35
  • HI @Free Radical ,Can i see the values stored in the $bin in a database table??
    – Vishnu
    Commented Jun 21, 2014 at 11:16
  • @Vishnu, Of course. The table has the same name as the $bin, and the string that makes up the $cid can be inspected in the field $cid. So the $cid 'mymodule:node_69' stored in the $bin 'cache' is stored in a table named "cache", and you should be able to see the 'mymodule:node_69' in $cid field. Then, the $bin 'cache_block' is stored in a table named "cache_block", etc. Note that field $data will hold serialized values, so you need to un-serialize to actually see the values. Commented Jun 21, 2014 at 12:49
  • HI @FREE RTADICAL ,sorry i have updated this question and made it as a seperate question "drupal.stackexchange.com/questions/120134/…" , I found that there were no tables instead it is using a memcache and is it an issue with memcache as i am new to memcache?,
    – Vishnu
    Commented Jun 23, 2014 at 6:05
  • Hi @Free Radical ,my issue is resolved ,i was using memcache module for caching.After cache_clear_all() function cacheflush time is set by memcache object in seconds ,then take_params() function contains cache_set() as it also sets cache created time in seconds,memcache will reject the cache if cacheflushtime is equalto or greater than cache created time, so if cache_clear and cache_set are running at same seconds ,then we won't get the cache while calling cache_get() ,i resolved the issue by adding sleep(1) after cache_clear all ,
    – Vishnu
    Commented Jul 5, 2014 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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