| 71 | /** |
| 72 | * Same as WP_Feed_Cache_Transient but use memcache as the backend |
| 73 | * In WP_Feed_Cache, it will only be activated when MEMCACHE_SEVER is defined in wp-config |
| 74 | */ |
| 75 | class WP_Feed_Cache_Memcache { |
| 76 | var $name; |
| 77 | var $mod_name; |
| 78 | var $lifetime = 43200; //Default lifetime in cache of 12 hours |
| 79 | var $memcache = null; |
| 80 | var $memcache_prefix = ""; |
| 81 | |
| 82 | function __construct($location, $filename, $extension) { |
| 83 | $this->name = 'feed_' . $filename; |
| 84 | $this->mod_name = 'feed_mod_' . $filename; |
| 85 | |
| 86 | $lifetime = $this->lifetime; |
| 87 | /** |
| 88 | * Filter the transient lifetime of the feed cache. |
| 89 | * |
| 90 | * @since 2.8.0 |
| 91 | * |
| 92 | * @param int $lifetime Cache duration in seconds. Default is 43200 seconds (12 hours). |
| 93 | * @param string $filename Unique identifier for the cache object. |
| 94 | */ |
| 95 | $this->lifetime = apply_filters( 'wp_feed_cache_transient_lifetime', $lifetime, $filename); |
| 96 | |
| 97 | $this->memcache = new Memcache(); |
| 98 | |
| 99 | // MEMCACHE_SERVER is from wp-config |
| 100 | $this->memcache->connect(MEMCACHE_SERVER, 11211); |
| 101 | $this->memcache_prefix = DB_NAME."::"; |
| 102 | } |
| 103 | |
| 104 | function save($data) { |
| 105 | if ( is_a($data, 'SimplePie') ) |
| 106 | $data = $data->data; |
| 107 | |
| 108 | $this->memcache->set($this->memcache_prefix . $this->name, $data, $this->lifetime); |
| 109 | $this->memcache->set($this->memcache_prefix . $this->mod_name, time(), $this->lifetime); |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | function load() { |
| 114 | return $this->memcache->get($this->memcache_prefix . $this->name); |
| 115 | } |
| 116 | |
| 117 | function mtime() { |
| 118 | return $this->memcache->get($this->memcache_prefix . $this->mod_name); |
| 119 | } |
| 120 | |
| 121 | function touch() { |
| 122 | return $this->memcache->set($this->memcache_prefix . $this->mod_name, time(), $this->lifetime); |
| 123 | } |
| 124 | |
| 125 | function unlink() { |
| 126 | $this->memcache->delete($this->memcache_prefix . $this->name); |
| 127 | $this->memcache->delete($this->memcache_prefix . $this->mod_name); |
| 128 | return true; |
| 129 | } |
| 130 | } |
| 131 | |