From 15973ec2b5939a3989af704c1c286dc740075c52 Mon Sep 17 00:00:00 2001 From: rynop Date: Fri, 27 Jan 2012 22:51:19 -0600 Subject: [PATCH] first pass before testing --- .gitignore | 4 + README | 55 ---------- README.md | 62 ++++++++++++ View/Helper/ViewMemcache.php | 35 +++++++ View/Helper/empty | 0 libs/cache/view_memcache.php | 166 ------------------------------- view_memcache_app_controller.php | 7 -- view_memcache_app_model.php | 7 -- views/helpers/view_memcache.php | 33 ------ 9 files changed, 101 insertions(+), 268 deletions(-) create mode 100644 .gitignore delete mode 100644 README create mode 100644 README.md create mode 100644 View/Helper/ViewMemcache.php create mode 100644 View/Helper/empty delete mode 100644 libs/cache/view_memcache.php delete mode 100644 view_memcache_app_controller.php delete mode 100644 view_memcache_app_model.php delete mode 100644 views/helpers/view_memcache.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6eceb33 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.buildpath +.project +.settings +*~ diff --git a/README b/README deleted file mode 100644 index 725a411..0000000 --- a/README +++ /dev/null @@ -1,55 +0,0 @@ -Configuration - -Put in APP/plugins/view_memcache - -This needs to go in app/bootstrap.php (rather than core.php as we're a plugin) - -Cache::config('view_memcache', array( - 'engine' => 'ViewMemcache', - 'duration'=> 60, - 'prefix' => '', - 'servers' => array( - '127.0.0.1:11211' - ), - 'compress' => false, -)); - - -Put this in APP/app_controller.php or your controller you want to do caching in. - -var $helpers = array('ViewMemcache.ViewMemcache'); - -To make it cache everything put - - $this->set('docache', true); - -In beforeFiler in APP/app_controller.php - -Or alternatively $this->set('docache', true); in each action. - -You can override the duration by setting: - - Configure::write('ViewMemcache.timeout', 6000); - -Or per action with: - - $this->set('docachetimeout', 4600); - -A simple Nginx config for you might be: - -location / { - set $memcached_key $request_uri; - memcached_connect_timeout 2000; - memcached_read_timeout 2000; - memcached_pass 127.0.0.1:11211; - default_type text/html; - # if memcache throws one of the following errors head off to - # the php-cgi - error_page 404 502 504 = @fallback; -} - -location @fallback{ - root /var/www/demo.*.com/app/webroot; - - try_files $uri $uri/ /index.php?url=$uri; -} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..df98ded --- /dev/null +++ b/README.md @@ -0,0 +1,62 @@ +# ViewMemcache + +Store your view(s) to memcache. Allows easy distributed view invalidation and serving pages right from memcache via nginx (for example) + +## Configuration + +clone into APP/Plugin/ViewMemcache + +Make an entry in APP/bootstrap.php for the 'view_memcache' engine: + +```php + Cache::config('view_memcache', array( + 'engine' => 'Memcache', //[required] + 'duration'=> 1209600, //2 weeks [optional] You can conditionally set this in your controller and or action, see below + 'probability'=> 100, //[optional] + 'prefix' => 'VM_', //[optional] prefix every cache file with this string. If you change, make sure to update your nginx conf + 'servers' => array( + '127.0.0.1:11211' // localhost, default port 11211 + ), //[optional] + 'persistent' => true, // [optional] set this to false for non-persistent connections + 'compress' => true, // [optional] compress data in Memcache (slower, but uses less memory) + )); + ``` + +Put this in APP/Controller/AppController.php or just in the controller you want to do caching in. + +```php +var $helpers = array('ViewMemcache.ViewMemcache'); +``` + +From controller (or beforeFilter(), or AppController::beforeFilter()) simply do: + +```php + $viewMemcacheTimeout = | ; //This is optional. If not set, will use + $enableViewMemcache = true; + $this->set(compact('viewMemcacheTimeout','enableViewMemcache')); +``` + +### nginx sample config + +location / { + set $memcached_key "VM_:$request_uri";; + memcached_connect_timeout 2000; + memcached_read_timeout 2000; + memcached_pass 127.0.0.1:11211; + default_type text/html; + # if memcache throws one of the following errors fallback to PHP + error_page 404 502 504 = @fallback; +} + +location @fallback{ + root /var/www/demo.*.com/app/webroot; + + try_files $uri $uri/ /index.php?$uri&$args; +} + +location ~ \.php$ { + fastcgi_pass 127.0.0.1:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_intercept_errors on; +} diff --git a/View/Helper/ViewMemcache.php b/View/Helper/ViewMemcache.php new file mode 100644 index 0000000..578203b --- /dev/null +++ b/View/Helper/ViewMemcache.php @@ -0,0 +1,35 @@ + | + * $enableViewMemcache = true; + * $this->set(compact('viewMemcacheTimeout','enableViewMemcache')); + * + * You can also disable storing view to memcache by setting Configure::write('ViewMemcache.disable',true); + * @author rynop + * + * Based on https://github.com/salgo/cakephp-viewmemcache by salgo + * + */ +class ViewMemcache extends AppHelper { + function afterLayout() { + if (Configure::read('Cache.disable') || Configure::read('ViewMemcache.disable')) { + return true; + } + + if (!empty($this->_View->viewVars['enableViewMemcache'] === true)) { + if (isset($this->_View->viewVars['viewMemcacheTimeout'])) { + Cache::set(array('duration' => $this->_View->viewVars['viewMemcacheTimeout'],null,'view_memcache')); //'+30 days' or seconds + } + + Cache::write($this->request->here, $this->_View->output, 'view_memcache'); + } + + return true; + } +} \ No newline at end of file diff --git a/View/Helper/empty b/View/Helper/empty new file mode 100644 index 0000000..e69de29 diff --git a/libs/cache/view_memcache.php b/libs/cache/view_memcache.php deleted file mode 100644 index 2a2ccd0..0000000 --- a/libs/cache/view_memcache.php +++ /dev/null @@ -1,166 +0,0 @@ - 127.0.0.1. If an - * array MemcacheEngine will use them as a pool. - * - compress = boolean, default => false - * - * @var array - * @access public - */ - - var $settings = array(); - -/** - * Initialize the Cache Engine - * - * Called automatically by the cache frontend - * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array()); - * - * @param array $setting array of setting for the engine - * @return boolean True if the engine has been successfully initialized, false if not - * @access public - */ - - function init($settings = array()) { - parent::init(array_merge(array( - 'engine'=> 'ViewMemcache', - 'prefix' => Inflector::slug(APP_DIR) . '_', - 'servers' => array('127.0.0.1'), - 'compress'=> false - ), $settings) - ); - - if ($this->settings['compress']) { - $this->settings['compress'] = false; - } - if (!is_array($this->settings['servers'])) { - $this->settings['servers'] = array($this->settings['servers']); - } - if (!isset($this->__Memcache)) { - $return = false; - $this->__Memcache =& new Memcache(); - foreach ($this->settings['servers'] as $server) { - $parts = explode(':', $server); - $host = $parts[0]; - $port = 11211; - if (isset($parts[1])) { - $port = $parts[1]; - } - if ($this->__Memcache->addServer($host, $port)) { - $this->__Memcache->setCompressThreshold(0, 1); - $return = true; - } - } - return $return; - } - return true; - } - -/** - * Write data for key into cache - * - * @param string $key Identifier for the data - * @param mixed $value Data to be cached - * @param integer $duration How long to cache the data, in seconds - * @return boolean True if the data was succesfully cached, false on failure - * @access public - */ - - function write($key, &$value, $duration) { - return @$this->__Memcache->set($key, $value, $this->settings['compress'], $duration); - } - - -/** - * Read a key from the cache - * - * @param string $key Identifier for the data - * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it - * @access public - */ - - function read($key) { - return @$this->__Memcache->get($key); - } - - -/** - * Delete a key from the cache - * - * @param string $key Identifier for the data - * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed - * @access public - */ - - function delete($key) { - return @$this->__Memcache->delete($key); - } - -/** - * Delete all keys from the cache - * - * @return boolean True if the cache was succesfully cleared, false otherwise - * @access public - */ - function clear() { - return @$this->__Memcache->flush(); - } - -/** - * Connects to a server in connection pool - * - * @param string $host host ip address or name - * @param integer $port Server port - * @return boolean True if memcache server was connected - * @access public - */ - function connect($host, $port = 11211) { - if ($this->__Memcache->getServerStatus($host, $port) === 0) { - if ($this->__Memcache->connect($host, $port)) { - return true; - } - return false; - } - return true; - } - -/** - * Use the key requested rather than doing any conversion on it - * - * @param string $key key - * @access public - */ - - function key($key) { - return $key; - } -} diff --git a/view_memcache_app_controller.php b/view_memcache_app_controller.php deleted file mode 100644 index 3a9640a..0000000 --- a/view_memcache_app_controller.php +++ /dev/null @@ -1,7 +0,0 @@ - \ No newline at end of file diff --git a/view_memcache_app_model.php b/view_memcache_app_model.php deleted file mode 100644 index 55ddaab..0000000 --- a/view_memcache_app_model.php +++ /dev/null @@ -1,7 +0,0 @@ - \ No newline at end of file diff --git a/views/helpers/view_memcache.php b/views/helpers/view_memcache.php deleted file mode 100644 index aa92a02..0000000 --- a/views/helpers/view_memcache.php +++ /dev/null @@ -1,33 +0,0 @@ -viewVars) && $view->viewVars['docache'] === true) { - $timeout = Configure::read('ViewMemcache.timeout'); - - if (array_key_exists('docachetimeout', $view->viewVars)) { - $timeout = $view->viewVars['docachetimeout']; - } - - $cache_footer = ''; - if (!array_key_exists('nocachefooter', $view->viewVars)) { - $cache_footer = "\n'; - } - - if ($timeout) { - Cache::set(array('duration' => $timeout)); - } - - Cache::write($view->here, $view->output . $cache_footer, 'view_memcache'); - } - - return true; - } -} \ No newline at end of file