1
andrey3761
XoopsCache class.

Hi guys,

I need 'XoopsCache' class documentation which is located in \class\cache\xoopscache.php. How do I use it with different types of cache like engine memcache, engine model etc.?

One more question, how do I choose a different caching type for Smarty templates e.g. engine memcache?

2
Dylian
Re: XoopsCache class.
  • 2009/12/22 22:51

  • Dylian

  • Friend of XOOPS

  • Posts: 237

  • Since: 2007/7/21


Quote:
I need 'XoopsCache' class documentation which is located in \class\cache\xoopscache.php.


Go to http://dev.xoofoo.org/dev_xoops_242/ then classes > XoopsCache .

Don't know if this is what you mean't by documentation...

Greets Dylian.

3
andrey3761
Re: XoopsCache class.

The example of operation with this class is necessary to me.

4
dbman
Re: XoopsCache class.
  • 2009/12/23 2:01

  • dbman

  • Friend of XOOPS

  • Posts: 172

  • Since: 2005/4/28


Check out one of the few usage examples in xoops:
- modules/system/class/gui/default/default.php
- admin.php

Also have a look at class/cache/memcache.php

There are some other classes of interest regarding xoopscache in the documentation as Dylan mentions.

Are you planning to implement memcache in a module or as a core hack?

5
andrey3761
Re: XoopsCache class.

Quote:
Are you planning to implement memcache in a module or as a core hack?


I wish to cache SMARTY templates in memcached. I wish to use in the modules memcached.

6
trabis
Re: XoopsCache class.
  • 2009/12/23 19:52

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


Has I understand you need to load class with:
xoops_load('cache') or xoops_load('xoopscache') for XOOPS 2.4.3.

Then you instantiate the class with:
$cache = XoopsCache::getInstance();

The you set the engine:
$cache->engine($name = 'file', $settings = array());

There are 5 engines available:
apc
file (default)
memcache
model(requires XOOPS 3.0)
xcache

You can use methods write, read, delete, clear.
You can get info about the available settings by looking into the engine classes (init method).

7
andrey3761
Re: XoopsCache class.

Help me.

I start this script, and in folder "/xoops_data/caches/xoops_cache" there is file "xoops_test_http%3A%2F%2Fradio-hobby_org.php", it cached in file, instead of in memcached. Where error? How to switch on cache in memcache?


include_once '../mainfile.php';

$val 'val';
$key 'test';

xoops_load('cache');
$cache XoopsCache::getInstance();
$cache->engine('memcache');
$cache->write($key$val100);
$ret $cache->read($key);
echo 
$ret;

?>

8
andrey3761
Re: XoopsCache class.

Has found function for caching Smarty of templates in memcached.

/**
 * Project: Smarty memcached cache handler function
 * Author: Mads Sülau Jørgensen 
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
function memcache_cache_handler($action, &$smarty_obj, &$cache_content$tpl_file=null$cache_id=null$compile_id=null$exp_time=null) {
    
// ref to the memcache object
    
$m $GLOBALS['memcached_res'];
    
    
// the key to store cache_ids under, used for clearing
    
$key 'smarty_caches';
    
    
// check memcache object
    
if (get_class($m) != 'Memcache') {
        
$smarty_obj->trigger_error('cache_handler: $GLOBALS['memcached_res'] is not a memcached object');
        return 
false;
    }
    
    
// unique cache id
    
$cache_id md5($tpl_file.$cache_id.$compile_id);
    
    switch (
$action) {
    case 
'read':
        
// grab the key from memcached
        
$contents $m->get($cache_id);
        
        
// use compression
        
if($smarty_obj->use_gzip && function_exists("gzuncompress")) {
            
$cache_content gzuncompress($contents);
        } else {
            
$cache_content $contents;
        }
        
        
$return true;
        break;
    
    case 
'write':
        
// use compression
        
if($smarty_obj->use_gzip && function_exists("gzcompress")) {
            
$contents gzcompress($cache_content);
        } else {
            
$contents $cache_content;
        }
        
        
// add the cache_id to the $key string
        
$caches $m->get($key);
        if (!
is_array($caches)) {
            
$caches = array($cache_id);
            
$m->set($key$caches);
        } else if (!
in_array($cache_id$caches)) {
            
array_push($caches$cache_id);
            
$m->set($key$caches);
        }
        
        
// store the value in memcached
        
$stored $m->set($cache_id$contents);
        
        if(!
$stored) {
            
$smarty_obj->trigger_error("cache_handler: set failed.");
        }
        
        
$return true;
        break;
    
    case 
'clear':
        if(empty(
$cache_id) && empty($compile_id) && empty($tpl_file)) {
            
// get all cache ids
            
$caches $m->get($key);
            
            if (
is_array($caches)) {
                
$len count($caches);
                for (
$i=0$i<$len$i++) {
                    
// assume no errors
                    
$m->delete($caches[$i]);
                }
                
                
// delete the cache ids
                
$m->delete($key);
                
                
$result true;
            }
        } else {
            
$result $m->delete($cache_id);
        }
        if(!
$result) {
            
$smarty_obj->trigger_error("cache_handler: query failed.");
        }
        
$return true;
        break;
        
    default:
        
// error, unknown action
        
$smarty_obj->trigger_error("cache_handler: unknown action "$action"");
        
$return false;
        break;
    }
    
    return 
$return;
}

?>


I apply it so:



include_once 'memcache_cache_handler.php';
//
$GLOBALS['memcached_res'] = new Memcache;
$GLOBALS['memcached_res']->connect('localhost'11211) or die ("Could not connect");
//

$xoopsTpl->cache_handler_func 'memcache_cache_handler';

if (!
$xoopsTpl->is_cached('db:shoutbox_shoutbox.html')) {
    
$result 'test';
    
$xoopsTpl->assign('shout'$result);
}
$xoopsTpl->display('db:shoutbox_shoutbox.html');
            
?>


How in this function correctly to specify $cache_id?

$cache_id md5($tpl_file.$cache_id.$compile_id);

9
Dylian
Re: XoopsCache class.
  • 2010/1/5 0:34

  • Dylian

  • Friend of XOOPS

  • Posts: 237

  • Since: 2007/7/21


Try this one, this way you'll immediately see if MEMCACHE is even available on your server.

require(../'mainfile.php');

include(
$GLOBALS['xoops']->path('header.php'));

if (
class_exists('Memcache')) {
    echo 
'The Memcache class is available'
;
}else{
    echo 
'The Memcache class is not available';
}

xoops_load('xoopscache'); 
$cache XoopsCache::getInstance(); 
if(
$cache->engine('memcache')){
    
$val 'val'
    
$key 'test'
    
$cache->write($key$val100); 
    
$ret $cache->read($key); 
    echo 
$ret
}else{
    echo 
'MEMCACHE Could not be loaded';
}

include(
$GLOBALS['xoops']->path('footer.php'));
?>
(Just a modified version of your original script)

Greets Dylian.

Login

Who's Online

368 user(s) are online (258 user(s) are browsing Support Forums)


Members: 0


Guests: 368


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits