Replacing wp_cache for phpFastCache

As I went over the documentation of wp_cache, i got everything set up so my project could benefit from it instead of always relying on mysql. What I didn’t know was the configuration part. I spent maybe 2 hours trying to figure how to make wp_cache work, and many solutions pointed to define(‘WP_CACHE’, true) within wp-config.php. I was able to have it defined, but it still failed to help wp_cache to work.

I’ve read also there are plugins out there which allow to cache pages, but my interest was to cache queries. I also noticed how people have faced issues with those plugins and how they don’t support newer versions. I needed a solution which was more generic and which could be applied to my work. So I searched for an alternative and I found phpFastCache.

Previously I made a facade class and in this way my application made no use of wp_cache. Here I show you how I switched from wp_cache to phpFastCache

//added file required at plugins’ main.php
require_once ‘php_fast_cache.php’;

Store_Caching.php

1
2
3
4
5
6
7
8
9
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Store_Caching
{
private static $group = "store_caching";
private static $keys = array('state'=>'state_sort', 'alphabet'=>'alphabet_sort', 'letter'=>'letter_sort');

public function get_cache( $type )
{
//$result = wp_cache_get( self::$keys[$type], self::$group ); --oldway
$result = phpFastCache::get( array( self::$group=>self::$keys[$type] ) );

error_log( 'from ' . self::$keys[$type] . ' ' . print_r($result, true) );

return $result;
}

public function set_cache( $type, $clear=false )
{

if( $clear )
{
//wp_cache_set( self::$keys[$type], array(), self::$group, 0 ); --old way
phpFastCache::set( array( self::$group=>self::$keys[$type] ), array(), 0 );
}
else
{
$result = array();

switch( $type )
{
case 'state':
$result = $this->cache_state();
break;
case 'alphabet':
$result = $this->cache_alphabet();
break;
case 'letter':
$result = $this->cache_letter();
break;
default:
$type = '';
}

if(!empty($type) )
{
//wp_cache_set( self::$keys[$type], $result, self::$group, 100 ); --old way
phpFastCache::set( array( self::$group=>self::$keys[$type] ), $result, 0 );
}

}

}

private function cache_state()
{
....
return $result;
}

private function cache_alphabet()
{
....
return $result;
}

private function cache_letter()
{
....
return $result;
}
}

2 Comments

  • Duy Tan
    July 30, 2014 - 8:08 am | Permalink

    //added file required at plugins’ main.php
    require_once ‘php_fast_cache.php’;
    Store_Caching.php

    why don’t you write where main.php and Store_Caching path ?

    • Juan Mendez
      July 30, 2014 - 5:05 pm | Permalink

      I remember for the project. I made a plugin for the client. And wanted to keep it encapsulated from the rest of wordpress by setting it at the plugin’s main.php. I used a facade pattern within client – model classes to retrieve content weather from the db or cached versions.

  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>