Using Memcache in PHPCMS v9

PHPCMS v9 is powerful indeed, but some built-in cache configuration methods lack clear tutorials, such as the memcache class.

PHPCMS has this cache built-in, but I never knew how to enable it.

After tinkering all night, I wanted to configure PHPCMS’s setcache and getcache methods to dynamically switch cache types, similar to ThinkPHP’s mechanism.

In the end, I reluctantly discovered that PHPCMS’s default development seems to use file-based storage exclusively.
So I had to add memcache or redis only where I needed it — modifying the kernel would be way too troublesome.

Configuration file:
cache/configs/cache.php

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
 
return array (
    'file1' => array (
        'type' => 'file',
        'debug' => true,
        'pconnect' => 0,
        'autoconnect' => 0
        ),
    'memcache' => array (
        'hostname' => '192.168.0.106',
        'port' => 11211,
        'timeout' => 3600,
        'type' => 'memcache',
        'debug' => true,
        'pconnect' => 0,
        'autoconnect' => 0
    ),
    'redis' => array (
        'hostname' => '192.168.0.106',
        'port' => 6379,
        'timeout' => 0,
        'type' => 'redis',
        'debug' => true,
        'pconnect' => 0,
        'autoconnect' => 0
    )
);
 
?>

From the configuration file format, it looks almost identical to TP (ThinkPHP), but it does not support dynamic switching.

Write it like this where needed. Here is a method that wraps record retrieval:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
 * Call an application detail
 */
function appdetail($appid, $ttl=0)
{
    if(FALSE == is_int($appid)) return FALSE;
        
    $cache_type = 'memcache';
    $cache_name = sprintf(&quot;app_detail_id_%d&quot;, $appid);
 
    $cache_config = pc_base::load_config('cache');
    
    /* Whether the system supports it */
    $cache_support = !empty($cache_config[$cache_type]) &amp;&amp; class_exists($cache_type);
 
    if($cache_support)
    {
        $memcache = cache_factory::get_instance($cache_config)->get_cache('memcache');
        $app = $memcache->get($cache_name);
        $app = unserialize($app);
    }else{
        $app = getcache($cache_name);
    }
    
    if($app === false)
    {
        $db = pc_base::load_model('apps_model');
        $app = $db->get_one(array(&quot;id&quot;=>$appid), &quot;id,name&quot;);
 
        if($cache_support)
        {
            $memcache = cache_factory::get_instance($cache_config)->get_cache('memcache');
            $memcache->set($cache_name, serialize($app), $ttl);
        }else{
            setcache($cache_name, $app, '', $ttl);
        }
    }
 
    return $app;
}

The general function is to detect whether the system supports the memcache class. If supported, use memcache directly; if not, fall back to PHPCMS’s built-in caching method.

Continuing to dig deeper into setcache,to see if my understanding was wrong

I spent the whole weekend tinkering at home, gradually getting familiar with PHPCMS’s MVC and template tag pc_tag, and I can now confidently conclude that:
PHPCMS caches all data entering the template layer. For example, data generated by all methods in content_tag.class.php will still be cached again into files.

When I use machine A to generate memcache data memcache_A1 and then call A1 through PHPCMS, a PHPCMS_A1 file is created on the PHPCMS disk. When my memcache_A1 changes, PHPCMS_A1 still reads from the PHPCMS_A1 cache.
The solution is to set the cache to 0 in pc_tag, which then allows using other caching tools on the backend!

Leave a Comment

Your email address will not be published.