1

First in mymodule.install I declared mymodule_schema(hook_schema) mymodule_install() and mymodule_unistall()

Where mymodule_schema() is like this:

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

this worked perfectly and table cache_mymodule with the same structue as table cache was created.
But in my code, using this:

$cid=some_calcuated_cid();
$cache=cache_get($cid,'cache_mymodule');
if (!$cache){
    $data = some_calcuated_data();
    cache_set($cid,$data,'cache_mymodule');
    dpm('CACHE SET');
else {
    dpm('used cache');
}

I always get 'CACHE SET', Now if i change 'cache_mymodule' to just cache, Or remove it, It works perfectly (saying used cache), Am I missing some hook in mymodule?

1
  • 1
    Since Drupal 7, you no longer need to drupal_install_schema on hook_install to add necessary tables. it will be done for you automagically.
    – AKS
    Nov 8, 2012 at 18:35

3 Answers 3

4

It looks like the problem is just a misnamed variable.

Surely

$data = some_calcuated_data();
cache_set($cid,$cache,'cache_mymodule');

should be

$data = some_calcuated_data();
cache_set($cid, $data, 'cache_mymodule');

At the moment you're setting the cache as NULL, any time it's already NULL...probably not what you were going for :)

3
  • fixed it, tnx, but it's correct in actual module
    – hkoosha
    Nov 7, 2012 at 16:59
  • Hmm very strange, I use exactly the same pattern all the time and I've never had a problem. Have you tried hardcoding a $cid to see if it's a problem with the id generation function?
    – Clive
    Nov 7, 2012 at 18:26
  • sorry Clive! really! you were right... sometimes people make crazy mistakes
    – hkoosha
    Dec 6, 2012 at 13:16
4

You don't need to create your own table to use the internal cache. Just use cache_set() and cache_get() as documented. This will store the $my_data in the default 'cache' table:

// Store $my_data in cache.
cache_set($data_id, $my_data);

// Get $my_data from cache.
$data = cache_get($data_id);

Your code snippet should look something like this:

$cid = some_calcuated_cid();
$cache = cache_get($cid);

if (!$cache){
  $data = some_calcuated_data();
  cache_set($cid, $data);
}

Documentation:

cache_set()

cache_get()

3
  • 4
    You can create custom cache tables if needed, no issues in that.
    – GoodSp33d
    Nov 7, 2012 at 7:07
  • i know but i want to learn creating my own cache, tnx for the tip
    – hkoosha
    Nov 7, 2012 at 16:56
  • Nothing wrong with that. I do realize I answered a bit beside the actual question. I would suggest you break things apart and make sure your variables contain correct data.
    – dxc
    Nov 7, 2012 at 20:43
0

In order to debug this, enable the mysql log which displays all queries executed (you will need a mysql restart)

mysqld.exe --log=path\to\log\file\your-log-file-name.log

This will give you a clue if the respective sql insert command is actually triggered and executed. And you should also check (via sql) if 'cache_mymodule' is empty or not after the code snippet execution.

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.