php-censor/src/B8Framework/Cache/RequestCache.php

91 lines
1.5 KiB
PHP
Raw Normal View History

2016-04-12 19:31:39 +02:00
<?php
namespace b8\Cache;
use b8\Type;
2017-01-09 14:29:04 +01:00
class RequestCache implements Type\CacheInterface
2016-04-12 19:31:39 +02:00
{
2016-04-20 12:30:26 +02:00
protected $data = [];
2016-04-12 19:31:39 +02:00
2016-04-20 12:30:26 +02:00
/**
* Check if caching is enabled.
*/
public function isEnabled()
{
return true;
}
2016-04-12 19:31:39 +02:00
2016-04-20 12:30:26 +02:00
/**
* Get item from the cache:
*/
public function get($key, $default = null)
{
return $this->contains($key) ? $this->data[$key] : $default;
}
2016-04-12 19:31:39 +02:00
2016-04-20 12:30:26 +02:00
/**
* Add an item to the cache:
*/
public function set($key, $value = null, $ttl = 0)
{
$this->data[$key] = $value;
2016-04-12 19:31:39 +02:00
2016-04-20 12:30:26 +02:00
return $this;
}
2016-04-12 19:31:39 +02:00
2016-04-20 12:30:26 +02:00
/**
* Remove an item from the cache:
*/
public function delete($key)
{
if ($this->contains($key)) {
unset($this->data[$key]);
}
return $this;
}
2016-04-12 19:31:39 +02:00
2016-04-20 12:30:26 +02:00
/**
* Check if an item is in the cache:
*/
public function contains($key)
{
return array_key_exists($key, $this->data);
}
2016-04-12 19:31:39 +02:00
2016-04-20 12:30:26 +02:00
/**
* Short-hand syntax for get()
* @see Config::get()
*/
2016-04-12 19:31:39 +02:00
public function __get($key)
{
return $this->get($key, null);
}
/**
2016-04-20 12:30:26 +02:00
* Short-hand syntax for set()
* @see Config::set()
*/
2016-04-12 19:31:39 +02:00
public function __set($key, $value = null)
{
return $this->set($key, $value);
}
/**
2016-04-20 12:30:26 +02:00
* Is set
*/
2016-04-12 19:31:39 +02:00
public function __isset($key)
{
return $this->contains($key);
}
/**
2016-04-20 12:30:26 +02:00
* Unset
*/
2016-04-12 19:31:39 +02:00
public function __unset($key)
{
$this->delete($key);
}
2016-04-20 12:30:26 +02:00
}