Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rynop
committed
Jan 28, 2012
1 parent
fc8cf79
commit 15973ec
Showing
9 changed files
with
101 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.buildpath | ||
.project | ||
.settings | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = <seconds> | <cache engine readable range, ex: '+30 days'>; //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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
App::uses('AppHelper', 'View/Helper'); | ||
|
||
/** | ||
* | ||
* Store view to 'view_memcache' memcache storage engine config | ||
* | ||
* From controller simply do: | ||
* $viewMemcacheTimeout = <seconds> | <cache engine readable range, ex: '+30 days'> | ||
* $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; | ||
} | ||
} |
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.