Packages

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

Classes

Interfaces

  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  * @package Nette\Caching\Storages
  7:  */
  8: 
  9: 
 10: 
 11: /**
 12:  * Memcached storage.
 13:  *
 14:  * @author     David Grudl
 15:  * @package Nette\Caching\Storages
 16:  */
 17: class NMemcachedStorage extends NObject implements ICacheStorage
 18: {
 19:     /** @internal cache structure */
 20:     const META_CALLBACKS = 'callbacks',
 21:         META_DATA = 'data',
 22:         META_DELTA = 'delta';
 23: 
 24:     /** @var Memcache */
 25:     private $memcache;
 26: 
 27:     /** @var string */
 28:     private $prefix;
 29: 
 30:     /** @var ICacheJournal */
 31:     private $journal;
 32: 
 33: 
 34:     /**
 35:      * Checks if Memcached extension is available.
 36:      * @return bool
 37:      */
 38:     public static function isAvailable()
 39:     {
 40:         return extension_loaded('memcache');
 41:     }
 42: 
 43: 
 44:     public function __construct($host = 'localhost', $port = 11211, $prefix = '', ICacheJournal $journal = NULL)
 45:     {
 46:         if (!self::isAvailable()) {
 47:             throw new NotSupportedException("PHP extension 'memcache' is not loaded.");
 48:         }
 49: 
 50:         $this->prefix = $prefix;
 51:         $this->journal = $journal;
 52:         $this->memcache = new Memcache;
 53:         if ($host) {
 54:             $this->addServer($host, $port);
 55:         }
 56:     }
 57: 
 58: 
 59:     public function addServer($host = 'localhost', $port = 11211, $timeout = 1)
 60:     {
 61:         if ($this->memcache->addServer($host, $port, TRUE, 1, $timeout) === FALSE) {
 62:             $error = error_get_last();
 63:             throw new InvalidStateException("Memcache::addServer(): $error[message].");
 64:         }
 65:     }
 66: 
 67: 
 68:     /**
 69:      * @return Memcache
 70:      */
 71:     public function getConnection()
 72:     {
 73:         return $this->memcache;
 74:     }
 75: 
 76: 
 77:     /**
 78:      * Read from cache.
 79:      * @param  string key
 80:      * @return mixed|NULL
 81:      */
 82:     public function read($key)
 83:     {
 84:         $key = urlencode($this->prefix . $key);
 85:         $meta = $this->memcache->get($key);
 86:         if (!$meta) {
 87:             return NULL;
 88:         }
 89: 
 90:         // meta structure:
 91:         // array(
 92:         //     data => stored data
 93:         //     delta => relative (sliding) expiration
 94:         //     callbacks => array of callbacks (function, args)
 95:         // )
 96: 
 97:         // verify dependencies
 98:         if (!empty($meta[self::META_CALLBACKS]) && !NCache::checkCallbacks($meta[self::META_CALLBACKS])) {
 99:             $this->memcache->delete($key, 0);
100:             return NULL;
101:         }
102: 
103:         if (!empty($meta[self::META_DELTA])) {
104:             $this->memcache->replace($key, $meta, 0, $meta[self::META_DELTA] + time());
105:         }
106: 
107:         return $meta[self::META_DATA];
108:     }
109: 
110: 
111:     /**
112:      * Prevents item reading and writing. Lock is released by write() or remove().
113:      * @param  string key
114:      * @return void
115:      */
116:     public function lock($key)
117:     {
118:     }
119: 
120: 
121:     /**
122:      * Writes item into the cache.
123:      * @param  string key
124:      * @param  mixed  data
125:      * @param  array  dependencies
126:      * @return void
127:      */
128:     public function write($key, $data, array $dp)
129:     {
130:         if (isset($dp[NCache::ITEMS])) {
131:             throw new NotSupportedException('Dependent items are not supported by MemcachedStorage.');
132:         }
133: 
134:         $key = urlencode($this->prefix . $key);
135:         $meta = array(
136:             self::META_DATA => $data,
137:         );
138: 
139:         $expire = 0;
140:         if (isset($dp[NCache::EXPIRATION])) {
141:             $expire = (int) $dp[NCache::EXPIRATION];
142:             if (!empty($dp[NCache::SLIDING])) {
143:                 $meta[self::META_DELTA] = $expire; // sliding time
144:             }
145:         }
146: 
147:         if (isset($dp[NCache::CALLBACKS])) {
148:             $meta[self::META_CALLBACKS] = $dp[NCache::CALLBACKS];
149:         }
150: 
151:         if (isset($dp[NCache::TAGS]) || isset($dp[NCache::PRIORITY])) {
152:             if (!$this->journal) {
153:                 throw new InvalidStateException('CacheJournal has not been provided.');
154:             }
155:             $this->journal->write($key, $dp);
156:         }
157: 
158:         $this->memcache->set($key, $meta, 0, $expire);
159:     }
160: 
161: 
162:     /**
163:      * Removes item from the cache.
164:      * @param  string key
165:      * @return void
166:      */
167:     public function remove($key)
168:     {
169:         $this->memcache->delete(urlencode($this->prefix . $key), 0);
170:     }
171: 
172: 
173:     /**
174:      * Removes items from the cache by conditions & garbage collector.
175:      * @param  array  conditions
176:      * @return void
177:      */
178:     public function clean(array $conditions)
179:     {
180:         if (!empty($conditions[NCache::ALL])) {
181:             $this->memcache->flush();
182: 
183:         } elseif ($this->journal) {
184:             foreach ($this->journal->clean($conditions) as $entry) {
185:                 $this->memcache->delete($entry, 0);
186:             }
187:         }
188:     }
189: 
190: }
191: 
Nette Framework 2.0.18 (for PHP 5.2, prefixed) API documentation generated by ApiGen 2.8.0