Skip to content
This repository
  • 1 commit
  • 1 file changed
  • 0 comments
  • 1 contributor

Showing 1 changed file with 170 additions and 0 deletions. Show diff stats Hide diff stats

  1. +170 0 libraries/lithium/storage/cache/adapter/WinCache.php
170 libraries/lithium/storage/cache/adapter/WinCache.php
... ... @@ -0,0 +1,170 @@
  1 +<?php
  2 +/**
  3 + * Lithium: the most rad php framework
  4 + *
  5 + * @copyright Copyright 2010, Union of RAD (http://union-of-rad.org)
  6 + * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7 + */
  8 +
  9 +namespace lithium\storage\cache\adapter;
  10 +
  11 +/**
  12 + * A WinCache cache adapter implementation.
  13 + *
  14 + * The WinCache cache adapter is meant to be used through the `Cache` interface,
  15 + * which abstracts away key generation, adapter instantiation and filter
  16 + * implementation.
  17 + *
  18 + * A simple configuration of this adapter can be accomplished in `app/config/bootstrap.php`
  19 + * as follows:
  20 + *
  21 + * {{{
  22 + * Cache::config(array(
  23 + * 'cache-config-name' => array('adapter' => 'WinCache')
  24 + * ));
  25 + * }}}
  26 + *
  27 + * This WinCache adapter provides basic support for `write`, `read`, `delete`
  28 + * and `clear` cache functionality, as well as allowing the first four
  29 + * methods to be filtered as per the Lithium filtering system. Additionally,
  30 + * This adapter defines several methods that are _not_ implemented in other
  31 + * adapters, and are thus non-portable - see the documentation for `Cache`
  32 + * as to how these methods should be accessed.
  33 + *
  34 + * This adapter supports multi-key `write`, `read` and `delete` operations.
  35 + *
  36 + * Learn more about WinCache in the
  37 + * [PHP WinCache manual](http://php.net/manual/en/book.wincache.php).
  38 + *
  39 + * @see lithium\storage\Cache::key()
  40 + */
  41 +class WinCache extends \lithium\core\Object {
  42 +
  43 + /**
  44 + * Class constructor
  45 + *
  46 + * @param array $config
  47 + * @return void
  48 + */
  49 + public function __construct(array $config = array()) {
  50 + $defaults = array(
  51 + 'prefix' => '',
  52 + 'expiry' => '+1 hour',
  53 + );
  54 + parent::__construct($config + $defaults);
  55 + }
  56 +
  57 + /**
  58 + * Write value(s) to the cache.
  59 + *
  60 + * This adapter method supports multi-key write. By specifying `$key` as an
  61 + * associative array of key/value pairs, `$data` is ignored and all keys that
  62 + * are cached will receive an expiration time of `$expiry`.
  63 + *
  64 + * @param string|array $key The key to uniquely identify the cached item.
  65 + * @param mixed $data The value to be cached.
  66 + * @param null|string $expiry A strtotime() compatible cache time. If no expiry time is set,
  67 + * then the default cache expiration time set with the cache configuration will be used.
  68 + * @return boolean True on successful write, false otherwise.
  69 + */
  70 + public function write($key, $data, $expiry = null) {
  71 + $expiry = ($expiry) ?: $this->_config['expiry'];
  72 +
  73 + return function($self, $params, $chain) use ($expiry) {
  74 + $cachetime = (is_int($expiry) ? $expiry : strtotime($expiry)) - time();
  75 + $key = $params['key'];
  76 +
  77 + if (is_array($key)) {
  78 + return wincache_ucache_add($key, $cachetime);
  79 + }
  80 + return wincache_ucache_add($params['key'], $params['data'], $cachetime);
  81 + };
  82 + }
  83 +
  84 + /**
  85 + * Read value(s) from the cache.
  86 + *
  87 + * This adapter method supports multi-key reads. By specifying `$key` as an
  88 + * array of key names, this adapter will attempt to return an array of data
  89 + * containing key/value pairs of the requested data.
  90 + *
  91 + * @param string|array $key The key to uniquely identify the cached item.
  92 + * @return mixed Cached value if successful, false otherwise.
  93 + */
  94 + public function read($key) {
  95 + return function($self, $params, $chain) {
  96 + return wincache_ucache_get($params['key']);
  97 + };
  98 + }
  99 +
  100 + /**
  101 + * Delete value from the cache.
  102 + *
  103 + * This adapter method supports multi-key deletes. By specifynig `$key` as an
  104 + * array of key names, this adapter method will attempt to remove these keys
  105 + * from the user space cache.
  106 + *
  107 + * @param string|array $key The key to uniquely identify the cached item.
  108 + * @return mixed True on successful delete, false otherwise.
  109 + */
  110 + public function delete($key) {
  111 + return function($self, $params, $chain) {
  112 + return wincache_ucache_delete($params['key']);
  113 + };
  114 + }
  115 +
  116 + /**
  117 + * Performs an atomic decrement operation on specified numeric cache item.
  118 + *
  119 + * Note that, as per the WinCache specification:
  120 + * If the item's value is not numeric, the decrement operation has no effect
  121 + * on the key - it retains it's original non-integer value.
  122 + *
  123 + * @param string $key Key of numeric cache item to decrement
  124 + * @param integer $offset Offset to decrement - defaults to 1.
  125 + * @return mixed Item's new value on successful decrement, false otherwise
  126 + */
  127 + public function decrement($key, $offset = 1) {
  128 + return function($self, $params, $chain) use ($offset) {
  129 + return wincache_ucache_dec($params['key'], $offset);
  130 + };
  131 + }
  132 +
  133 + /**
  134 + * Performs an atomic increment operation on specified numeric cache item.
  135 + *
  136 + * Note that, as per the WinCache specification:
  137 + * If the item's value is not numeric, the increment operation has no effect
  138 + * on the key - it retains it's original non-integer value.
  139 + *
  140 + * @param string $key Key of numeric cache item to increment
  141 + * @param integer $offset Offset to increment - defaults to 1.
  142 + * @return mixed Item's new value on successful increment, false otherwise
  143 + */
  144 + public function increment($key, $offset = 1) {
  145 + return function($self, $params, $chain) use ($offset) {
  146 + return wincache_ucache_inc($params['key'], $offset);
  147 + };
  148 + }
  149 +
  150 + /**
  151 + * Clears user-space cache
  152 + *
  153 + * @return mixed True on successful clear, false otherwise
  154 + */
  155 + public function clear() {
  156 + return wincache_ucache_clear('user');
  157 + }
  158 +
  159 + /**
  160 + * Determines if the WinCache extension has been installed and
  161 + * if the userspace cache is available.
  162 + *
  163 + * return boolean True if enabled, false otherwise
  164 + */
  165 + public static function enabled() {
  166 + return extension_loaded('wincache');
  167 + }
  168 +}
  169 +
  170 +?>

No commit comments for this range

Something went wrong with that request. Please try again.