Packages

  • Nette
    • Application
      • Application\Diagnostics
      • Application\Responses
      • Application\Routers
      • Application\UI
    • Caching
      • Caching\Storages
    • ComponentModel
    • Config
    • Database
      • Database\Diagnostics
      • Database\Drivers
      • Database\Reflection
      • Database\Table
    • DI
    • Diagnostics
    • Forms
      • Forms\Controls
      • Forms\Rendering
    • Http
    • Iterators
    • Latte
      • Latte\Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • None
  • PHP

Classes

  • NDevNullStorage
  • NFileJournal
  • NFileStorage
  • NMemcachedStorage
  • NMemoryStorage
  • NPhpFileStorage

Interfaces

  • ICacheJournal
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004, 2011 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:  * @package Nette\Caching\Storages
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Memcached storage.
 17:  *
 18:  * @author     David Grudl
 19:  * @package Nette\Caching\Storages
 20:  */
 21: class NMemcachedStorage extends NObject implements ICacheStorage
 22: {
 23:     /** @internal cache structure */
 24:     const META_CALLBACKS = 'callbacks',
 25:         META_DATA = 'data',
 26:         META_DELTA = 'delta';
 27: 
 28:     /** @var Memcache */
 29:     private $memcache;
 30: 
 31:     /** @var string */
 32:     private $prefix;
 33: 
 34:     /** @var ICacheJournal */
 35:     private $journal;
 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 = '', ICacheJournal $journal = NULL)
 51:     {
 52:         if (!self::isAvailable()) {
 53:             throw new NotSupportedException("PHP extension 'memcache' is not loaded.");
 54:         }
 55: 
 56:         $this->prefix = $prefix;
 57:         $this->journal = $journal;
 58:         $this->memcache = new Memcache;
 59:         NDebugger::tryError();
 60:         $this->memcache->connect($host, $port);
 61:         if (NDebugger::catchError($e)) {
 62:             throw new InvalidStateException('Memcache::connect(): ' . $e->getMessage(), 0, $e);
 63:         }
 64:     }
 65: 
 66: 
 67: 
 68:     /**
 69:      * Read from cache.
 70:      * @param  string key
 71:      * @return mixed|NULL
 72:      */
 73:     public function read($key)
 74:     {
 75:         $key = $this->prefix . $key;
 76:         $meta = $this->memcache->get($key);
 77:         if (!$meta) {
 78:             return NULL;
 79:         }
 80: 
 81:         // meta structure:
 82:         // array(
 83:         //     data => stored data
 84:         //     delta => relative (sliding) expiration
 85:         //     callbacks => array of callbacks (function, args)
 86:         // )
 87: 
 88:         // verify dependencies
 89:         if (!empty($meta[self::META_CALLBACKS]) && !NCache::checkCallbacks($meta[self::META_CALLBACKS])) {
 90:             $this->memcache->delete($key, 0);
 91:             return NULL;
 92:         }
 93: 
 94:         if (!empty($meta[self::META_DELTA])) {
 95:             $this->memcache->replace($key, $meta, 0, $meta[self::META_DELTA] + time());
 96:         }
 97: 
 98:         return $meta[self::META_DATA];
 99:     }
100: 
101: 
102: 
103:     /**
104:      * Writes item into the cache.
105:      * @param  string key
106:      * @param  mixed  data
107:      * @param  array  dependencies
108:      * @return void
109:      */
110:     public function write($key, $data, array $dp)
111:     {
112:         if (isset($dp[NCache::ITEMS])) {
113:             throw new NotSupportedException('Dependent items are not supported by MemcachedStorage.');
114:         }
115: 
116:         $key = $this->prefix . $key;
117:         $meta = array(
118:             self::META_DATA => $data,
119:         );
120: 
121:         $expire = 0;
122:         if (isset($dp[NCache::EXPIRATION])) {
123:             $expire = (int) $dp[NCache::EXPIRATION];
124:             if (!empty($dp[NCache::SLIDING])) {
125:                 $meta[self::META_DELTA] = $expire; // sliding time
126:             }
127:         }
128: 
129:         if (isset($dp[NCache::CALLBACKS])) {
130:             $meta[self::META_CALLBACKS] = $dp[NCache::CALLBACKS];
131:         }
132: 
133:         if (isset($dp[NCache::TAGS]) || isset($dp[NCache::PRIORITY])) {
134:             if (!$this->journal) {
135:                 throw new InvalidStateException('CacheJournal has not been provided.');
136:             }
137:             $this->journal->write($key, $dp);
138:         }
139: 
140:         $this->memcache->set($key, $meta, 0, $expire);
141:     }
142: 
143: 
144: 
145:     /**
146:      * Removes item from the cache.
147:      * @param  string key
148:      * @return void
149:      */
150:     public function remove($key)
151:     {
152:         $this->memcache->delete($this->prefix . $key, 0);
153:     }
154: 
155: 
156: 
157:     /**
158:      * Removes items from the cache by conditions & garbage collector.
159:      * @param  array  conditions
160:      * @return void
161:      */
162:     public function clean(array $conds)
163:     {
164:         if (!empty($conds[NCache::ALL])) {
165:             $this->memcache->flush();
166: 
167:         } elseif ($this->journal) {
168:             foreach ($this->journal->clean($conds) as $entry) {
169:                 $this->memcache->delete($entry, 0);
170:             }
171:         }
172:     }
173: 
174: }
175: 
Nette Framework 2.0beta1 (for PHP 5.2) API API documentation generated by ApiGen 2.3.0