Guest User

Untitled

a guest
Jun 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. <?php
  2.  
  3. interface haloadmin_cache_ICache {
  4. public function exists($key);
  5. public function get($key);
  6. public function set($key, $value = null, $expires = null);
  7. public function delete($key);
  8. }
  9.  
  10. $cache1 = new haloadmin_cache_SimpleCache();
  11. $cache2 = new haloadmin_cache_RedisCache();
  12.  
  13. cache_this($cache1, 'hello', 'world');
  14. cache_this($cache2, 'hello', 'world');
  15.  
  16. function cache_this(haloadmin_cache_ICache $cache, $key, $value = null) {
  17. $cache->set($key, $value);
  18. }
  19.  
  20. abstract class haloadmin_cache_AbstractClass implements haloadmin_cache_ICache {
  21. final public function get($key) { return unserialize($this->getRaw($key)); }
  22. final public function set($key, $value = null, $expires = null) {
  23. $this->setRaw($key, serialize($value), $expires);
  24. }
  25. abstract public function getRaw($key);
  26. abstract public function setRaw($key, $value = null, $expires = null);
  27. }
  28.  
  29. class haloadmin_cache_SimpleCache extends haloadmin_cache_AbstractClass {
  30. public function exists($key) { return isset($this->map[$key]); }
  31. public function delete($key) { unset($this->map[$key]; }
  32. public function getRaw($key) { return $this->map[$key]; }
  33. public function setRaw($key, $value = null, $expires = null) {
  34. $this->map[$key] = $value;
  35. }
  36. }
  37.  
  38.  
  39. require_once('haloadmin_cache_AbstractCache.php');
  40. require_once('Predis.php');
  41. require_once('dd_logging_LogFactory.php');
  42. class haloadmin_cache_RedisCache extends haloadmin_cache_AbstractClass {
  43. /**
  44. * @var dd_logging_ILogger
  45. */
  46. static public $LOGGER;
  47. public function __construct($url = null, $auth = null) {
  48. $this->redis = Predis\Client::create($url);
  49. }
  50. public function exists($key) { return $this->redis->exists($key); }
  51. public function delete($key) { $this->redis->delete($key); }
  52. public function getRaw($key) { return $this->redis->get($key); }
  53. public function setRaw($key, $value = null, $expires = null) {
  54. $this->redis->set($key, $value);
  55. if ( $expires ) {
  56. if ( self::$LOGGER->isInfoEnabled() ) {
  57. self::$LOGGER->info('Setting "' . $key . '" to expire in ' . $expires . ' seconds');
  58. }
  59. $this->redis->expire($key, $expires);
  60. }
  61. }
  62. }
  63. haloadmin_cache_RedisCache::$LOGGER = dd_logging_LogFactory::get('haloadmin_cache_RedisCache');
Add Comment
Please, Sign In to add comment