예제 #1
0
 public function testItCanGetAndReturnsValueWithoutTtl()
 {
     $this->cache->set('cached.value.key', 1);
     InMemoryAdapter::getInstance()->drop();
     $this->assertEquals(1, $this->cache->get('cached.value.key'));
     $this->assertTrue($this->cache->isHit());
 }
예제 #2
0
 /**
  * @param CacheAdapter $cache
  * @param string $namespace
  * @param int $expires
  */
 public function __construct(CacheAdapter $cache = null, $namespace = '', $expires = 0)
 {
     $this->cache = null === $cache ? InMemoryAdapter::getInstance() : $cache;
     $this->namespace = empty($namespace) ? '' : $namespace . ".";
     if (0 !== $expires) {
         $this->defaultTtl($expires);
     }
 }
예제 #3
0
 /**
  * @param array           $connections
  * @param CacheAdapter    $next
  */
 public function __construct(array $connections, CacheAdapter $next = null)
 {
     $this->nextAdapter = InMemoryAdapter::getInstance() === $next ? null : $next;
     try {
         $this->connected = true;
         $this->redis = new Client($connections);
         $this->redis->connect();
     } catch (\Exception $e) {
         $this->connected = false;
     }
 }
예제 #4
0
 /**
  * @param array           $connections
  * @param CacheAdapter    $next
  *
  * @throws \Exception
  */
 public function __construct(array $connections, CacheAdapter $next = null)
 {
     $this->isRedisExtensionAvailable();
     try {
         $connections = \array_values($connections);
         $this->connected = true;
         $this->redis = new Redis();
         $this->redis->connect($connections[0]['host'], $connections[0]['port'], $connections[0]['timeout']);
         $this->redis->select($connections[0]['database']);
     } catch (RedisException $e) {
         $this->connected = false;
     }
     $this->nextAdapter = InMemoryAdapter::getInstance() === $next ? null : $next;
 }
예제 #5
0
 /**
  * Get a value identified by $key.
  *
  * @param  string $key
  *
  * @return mixed
  */
 public function get($key)
 {
     $this->hit = false;
     $inMemoryValue = InMemoryAdapter::getInstance()->get($key);
     if (InMemoryAdapter::getInstance()->isHit()) {
         $this->hit = true;
         return $inMemoryValue;
     }
     $value = $this->memcached->get($key);
     if ($value) {
         $this->hit = true;
         InMemoryAdapter::getInstance()->set($key, $value, 0);
         return $value;
     }
     return null !== $this->nextAdapter ? $this->nextAdapter->get($key) : null;
 }
예제 #6
0
 /**
  * Get a value identified by $key.
  *
  * @param  string $key
  *
  * @return bool|mixed
  */
 public function get($key)
 {
     $key = (string) $key;
     $this->hit = false;
     $inMemoryValue = InMemoryAdapter::getInstance()->get($key);
     if (InMemoryAdapter::getInstance()->isHit()) {
         $this->hit = true;
         return $inMemoryValue;
     }
     $value = $this->curl->get($key);
     if (null !== $value) {
         $this->hit = true;
         InMemoryAdapter::getInstance()->set($key, $value, 0);
         return $this->restoreDataStructure($value);
     }
     return null !== $this->nextAdapter ? $this->nextAdapter->get($key) : null;
 }
예제 #7
0
 /**
  * Get a value identified by $key.
  *
  * @param  string $key
  *
  * @return bool|mixed
  */
 public function get($key)
 {
     $this->hit = false;
     $inMemoryValue = InMemoryAdapter::getInstance()->get($key);
     if (InMemoryAdapter::getInstance()->isHit()) {
         $this->hit = true;
         return $inMemoryValue;
     }
     if ($this->isAvailable()) {
         $value = $this->redis->get($key);
         if ($value) {
             $this->hit = true;
             $value = $this->restoreDataStructure($value);
             InMemoryAdapter::getInstance()->set($key, $value, 0);
             return $value;
         }
     }
     return null !== $this->nextAdapter ? $this->nextAdapter->get($key) : null;
 }
예제 #8
0
 /**
  * Get a value identified by $key.
  *
  * @param  string $key
  *
  * @return bool|mixed
  */
 public function get($key)
 {
     $key = (string) $key;
     $this->hit = false;
     $inMemoryValue = InMemoryAdapter::getInstance()->get($key);
     if (InMemoryAdapter::getInstance()->isHit()) {
         $this->hit = true;
         return $inMemoryValue;
     }
     $fileKey = $this->getFilenameFromCacheKey($key);
     if (true === \file_exists($fileKey)) {
         $value = $this->restoreDataStructure(\file_get_contents($fileKey));
         if ($value['expires'] >= new DateTime()) {
             $this->hit = true;
             InMemoryAdapter::getInstance()->set($key, $value['value'], 0);
             return $value['value'];
         }
         $this->removeCacheFile($fileKey);
         $this->deleteChain($key);
     }
     return null !== $this->nextAdapter ? $this->nextAdapter->get($key) : null;
 }
예제 #9
0
 /**
  * @param $key
  */
 protected function deleteChain($key)
 {
     InMemoryAdapter::getInstance()->delete($key);
     if (null !== $this->nextAdapter) {
         $this->nextAdapter->delete($key);
     }
 }
예제 #10
0
 protected function setUp()
 {
     $connections = [['host' => '255.0.0.0', 'port' => 6379, 'database' => 1, 'alias' => 'cache1', 'timeout' => 1]];
     $nextAdapter = InMemoryAdapter::getInstance();
     $this->cache = new DummyAdapter($connections, $nextAdapter);
 }
예제 #11
0
 public function testItWillReturnEmptyForGetIfException()
 {
     $this->forceDummyPDOConnectionToFireExceptions();
     InMemoryAdapter::getInstance()->drop();
     $this->assertEquals(null, $this->cache->get('cached.value.key'));
 }
예제 #12
0
 protected function setUp()
 {
     $this->nextAdapter = InMemoryAdapter::getInstance();
 }
예제 #13
0
 public function testItCanCacheAnArray()
 {
     $data = [new DateTime('now')];
     $this->cache->set('cached.value.key', $data, 1);
     $this->assertEquals($data, $this->cache->get('cached.value.key'));
 }
 /**
  *
  */
 protected function setUp()
 {
     $this->nextAdapter = InMemoryAdapter::getInstance();
     $baseUrl = 'http://localhost:9200';
     $this->cache = new DummyElasticSearchAdapter($baseUrl, 'cache', $this->nextAdapter);
 }
예제 #15
0
 public function testItCanGetAndReturnsValueFromFileSystemAndWillExpire()
 {
     $this->cache->set('cached.value.key', 1, 1);
     InMemoryAdapter::getInstance()->drop();
     \sleep(2);
     //Not a bug, Wait for 2 seconds.
     $this->assertEquals(null, $this->cache->get('cached.value.key'));
     $this->assertFalse($this->cache->isHit());
 }
예제 #16
0
 /**
  * Get a value identified by $key.
  *
  * @param  string $key
  *
  * @return bool|mixed
  */
 public function get($key)
 {
     $this->hit = false;
     $inMemoryValue = InMemoryAdapter::getInstance()->get($key);
     if (InMemoryAdapter::getInstance()->isHit()) {
         $this->hit = true;
         return $inMemoryValue;
     }
     $result = $this->getFromDatabase($key);
     if (false === empty($result)) {
         $ttl = new DateTime($result[self::CACHE_TTL]);
         if ($ttl >= new DateTime()) {
             $this->hit = true;
             $value = $this->restoreDataStructure($result[self::CACHE_VALUE]);
             InMemoryAdapter::getInstance()->set($key, $value, 0);
             return $value;
         }
         $this->delete($key);
     }
     return null !== $this->nextAdapter ? $this->nextAdapter->get($key) : null;
 }
예제 #17
0
 public function testItReturnsTtlValueFromDefaultTtl()
 {
     $this->cache->defaultTtl(10);
     $this->cache->set('cache.key', 1);
     $this->assertEquals(1, $this->cache->get('cache.key'));
 }
예제 #18
0
use NilPortugues\Cache\Adapter\PostgreSqlAdapter;
use NilPortugues\Cache\Adapter\RedisAdapter;
use NilPortugues\Cache\Adapter\PredisAdapter;
use NilPortugues\Cache\Adapter\MySqlAdapter;
use NilPortugues\Cache\Adapter\SphinxAdapter;
use NilPortugues\Cache\Adapter\SqliteAdapter;
use NilPortugues\Cache\Cache;
$parameters = (include_once realpath(dirname(__FILE__)) . '/cache_parameters.php');
/******************************************************
 * Caching in system memory or disk
 ******************************************************/
$fileSystemAdapter = new FileSystemAdapter($parameters['filesystem']['path']);
/*******************************************************
 * Caching using cache systems
 ******************************************************/
$nativeRedisAdapter = new RedisAdapter($parameters['redis']);
$predisRedisAdapter = new PredisAdapter($parameters['redis']);
$memcachedAdapter = new MemcachedAdapter($parameters['memcached']['persistent_id'], $parameters['memcached']['connections']);
/*******************************************************
 * Caching using the database
 ******************************************************/
$mysqlAdapter = new MySqlAdapter($parameters['mysql']['connections'], $parameters['mysql']['cache_table']);
$postgresqlAdapter = new PostgreSqlAdapter($parameters['postgresql']['connections'], $parameters['postgresql']['cache_table']);
$sqliteAdapter = new SqliteAdapter($parameters['sqlite']['connections'], $parameters['sqlite']['cache_table']);
/*******************************************************
 * Caching using full-text engines
 ******************************************************/
$sphinxAdapter = new SphinxAdapter($parameters['sphinx']['connections'], $parameters['sphinx']['cache_table']);
$elasticSearchAdapter = new ElasticSearchAdapter($parameters['elastic']['base_url'], $parameters['elastic']['index_name']);
return ['nil_portugues.cache.adapter.in_memory_adapter' => InMemoryAdapter::getInstance(), 'nil_portugues.cache.adapter.file_system_adapter' => $fileSystemAdapter, 'nil_portugues.cache.adapter.redis.native_adapter' => $nativeRedisAdapter, 'nil_portugues.cache.adapter.redis.predis_adapter' => $predisRedisAdapter, 'nil_portugues.cache.adapter.memcached_adapter' => $memcachedAdapter, 'nil_portugues.cache.adapter.sql.mysql_adapter' => $mysqlAdapter, 'nil_portugues.cache.adapter.sql.postgresql_adapter' => $postgresqlAdapter, 'nil_portugues.cache.adapter.sql.sqlite_adapter' => $sqliteAdapter, 'nil_portugues.cache.adapter.sql.sphinx_adapter' => $sphinxAdapter, 'nil_portugues.cache.adapter.sql.elastic_adapter' => $elasticSearchAdapter, 'nil_portugues.user_cache' => new Cache($nativeRedisAdapter, 'user'), 'nil_portugues.image_cache' => new Cache($nativeRedisAdapter, 'image')];
예제 #19
0
 public function testItShouldSetExpires()
 {
     $cache = new Cache(InMemoryAdapter::getInstance(), 'user', 1);
     \sleep(1);
     $this->assertEquals(null, $cache->get('some.key'));
 }