Namespaces

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • Cache
  • DummyStorage
  • FileStorage
  • MemcachedStorage

Interfaces

  • ICacheStorage
  • Overview
  • Namespace
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\Caching;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18: /**
 19:  * Memcached storage.
 20:  *
 21:  * @author     David Grudl
 22:  */
 23: class MemcachedStorage extends Nette\Object implements ICacheStorage
 24: {
 25:     /**#@+ @internal cache structure */
 26:     const META_CALLBACKS = 'callbacks';
 27:     const META_DATA = 'data';
 28:     const META_DELTA = 'delta';
 29:     /**#@-*/
 30: 
 31:     /** @var Memcache */
 32:     private $memcache;
 33: 
 34:     /** @var string */
 35:     private $prefix;
 36: 
 37: 
 38: 
 39:     /**
 40:      * Checks if Memcached extension is available.
 41:      * @return bool
 42:      */
 43:     public static function isAvailable()
 44:     {
 45:         return extension_loaded('memcache');
 46:     }
 47: 
 48: 
 49: 
 50:     public function __construct($host = 'localhost', $port = 11211, $prefix = '')
 51:     {
 52:         if (!self::isAvailable()) {
 53:             throw new \Exception("PHP extension 'memcache' is not loaded.");
 54:         }
 55: 
 56:         $this->prefix = $prefix;
 57:         $this->memcache = new \Memcache;
 58:         $this->memcache->connect($host, $port);
 59:     }
 60: 
 61: 
 62: 
 63:     /**
 64:      * Read from cache.
 65:      * @param  string key
 66:      * @return mixed|NULL
 67:      */
 68:     public function read($key)
 69:     {
 70:         $key = $this->prefix . $key;
 71:         $meta = $this->memcache->get($key);
 72:         if (!$meta) return NULL;
 73: 
 74:         // meta structure:
 75:         // array(
 76:         //     data => stored data
 77:         //     delta => relative (sliding) expiration
 78:         //     callbacks => array of callbacks (function, args)
 79:         // )
 80: 
 81:         // verify dependencies
 82:         if (!empty($meta[self::META_CALLBACKS]) && !Cache::checkCallbacks($meta[self::META_CALLBACKS])) {
 83:             $this->memcache->delete($key, 0);
 84:             return NULL;
 85:         }
 86: 
 87:         if (!empty($meta[self::META_DELTA])) {
 88:             $this->memcache->replace($key, $meta, 0, $meta[self::META_DELTA] + time());
 89:         }
 90: 
 91:         return $meta[self::META_DATA];
 92:     }
 93: 
 94: 
 95: 
 96:     /**
 97:      * Writes item into the cache.
 98:      * @param  string key
 99:      * @param  mixed  data
100:      * @param  array  dependencies
101:      * @return void
102:      */
103:     public function write($key, $data, array $dp)
104:     {
105:         if (!empty($dp[Cache::TAGS]) || isset($dp[Cache::PRIORITY]) || !empty($dp[Cache::ITEMS])) {
106:             throw new \NotSupportedException('Tags, priority and dependent items are not supported by MemcachedStorage.');
107:         }
108: 
109:         $meta = array(
110:             self::META_DATA => $data,
111:         );
112: 
113:         $expire = 0;
114:         if (isset($dp[Cache::EXPIRATION])) {
115:             $expire = (int) $dp[Cache::EXPIRATION];
116:             if (!empty($dp[Cache::SLIDING])) {
117:                 $meta[self::META_DELTA] = $expire; // sliding time
118:             }
119:         }
120: 
121:         if (isset($dp[Cache::CALLBACKS])) {
122:             $meta[self::META_CALLBACKS] = $dp[Cache::CALLBACKS];
123:         }
124: 
125:         $this->memcache->set($this->prefix . $key, $meta, 0, $expire);
126:     }
127: 
128: 
129: 
130:     /**
131:      * Removes item from the cache.
132:      * @param  string key
133:      * @return void
134:      */
135:     public function remove($key)
136:     {
137:         $this->memcache->delete($this->prefix . $key, 0);
138:     }
139: 
140: 
141: 
142:     /**
143:      * Removes items from the cache by conditions & garbage collector.
144:      * @param  array  conditions
145:      * @return void
146:      */
147:     public function clean(array $conds)
148:     {
149:         if (!empty($conds[Cache::ALL])) {
150:             $this->memcache->flush();
151: 
152:         } elseif (isset($conds[Cache::TAGS]) || isset($conds[Cache::PRIORITY])) {
153:             throw new \NotSupportedException('Tags and priority is not supported by MemcachedStorage.');
154:         }
155:     }
156: 
157: }
158: 
Nette Framework 0.9.7 API documentation generated by ApiGen 2.3.0