Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/Cache/ListGenerator.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/***************************************************************************
* Copyright (C) 2012 by Artem Naumenko *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/

/**
* @param string $key
*
* @return Listable
*/

interface ListGenerator
{
public function fetchList($key);
}
65 changes: 47 additions & 18 deletions core/DB/NoSQL/RedisNoSQL.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
* *
***************************************************************************/

final class RedisNoSQL extends CachePeer
final class RedisNoSQL extends CachePeer implements ListGenerator
{
const DEFAULT_HOST = 'localhost';
const DEFAULT_PORT = '6379';
const DEFAULT_TIMEOUT = 1.0;

private $redis = null;
private $host = null;
private $port = null;
private $timeout = null;
private $redis = null;
private $host = null;
private $port = null;
private $timeout = null;
private $triedConnect = false;

/**
* @param type $host
Expand All @@ -44,15 +45,6 @@ public function __construct(
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;

$this->redis = new Redis();

try {
$this->redis->pconnect($this->host, $this->port, $this->timeout);
$this->isAlive();
} catch (RedisException $e) {
$this->alive = false;
}
}

public function __destruct()
Expand All @@ -68,6 +60,8 @@ public function __destruct()

public function clean()
{
$this->ensureTriedToConnect();

try {
$this->redis->flushDB();
} catch (RedisException $e) {
Expand All @@ -79,6 +73,8 @@ public function clean()

public function isAlive()
{
$this->ensureTriedToConnect();

try {
$this->alive = $this->redis->ping() == '+PONG';
} catch (RedisException $e) {
Expand All @@ -90,6 +86,8 @@ public function isAlive()

public function append($key, $data)
{
$this->ensureTriedToConnect();

try {
return $this->redis->append($key, $data);
} catch (RedisException $e) {
Expand All @@ -99,6 +97,8 @@ public function append($key, $data)

public function decrement($key, $value)
{
$this->ensureTriedToConnect();

try {
return $this->redis->decrBy($key, $value);
} catch (RedisException $e) {
Expand All @@ -108,6 +108,8 @@ public function decrement($key, $value)

public function delete($key)
{
$this->ensureTriedToConnect();

try {
return $this->redis->delete($key);
} catch (RedisException $e) {
Expand All @@ -117,6 +119,8 @@ public function delete($key)

public function get($key)
{
$this->ensureTriedToConnect();

try {
return $this->redis->get($key);
} catch (RedisException $e) {
Expand All @@ -128,6 +132,8 @@ public function get($key)

public function increment($key, $value)
{
$this->ensureTriedToConnect();

try {
return $this->redis->incrBy($key, $value);
} catch (RedisException $e) {
Expand All @@ -140,15 +146,17 @@ public function increment($key, $value)
*
* @return RedisNoSQLList
*/
public function fetchList($key)
public function fetchList($key, $timeout = null)
{
return new RedisNoSQLList($this->redis, $key);
$this->ensureTriedToConnect();

return new RedisNoSQLList($this->redis, $key, $timeout);
}

/**
* @param string $key
*
* @return ISet
* @return RedisNoSQLSet
*/
public function fetchSet($key)
{
Expand All @@ -158,7 +166,7 @@ public function fetchSet($key)
/**
* @param string $key
*
* @return IHash
* @return RedisNoSQLHash
*/
public function fetchHash($key)
{
Expand All @@ -167,6 +175,8 @@ public function fetchHash($key)

protected function store($action, $key, $value, $expires = Cache::EXPIRES_MEDIUM)
{
$this->ensureTriedToConnect();

switch ($action) {
case 'set':
case 'replace':
Expand All @@ -181,5 +191,24 @@ protected function store($action, $key, $value, $expires = Cache::EXPIRES_MEDIUM
throw new UnimplementedFeatureException();
}
}

protected function ensureTriedToConnect()
{
if ($this->triedConnect)
return $this;

$this->triedConnect = true;

$this->redis = new Redis();

try {
$this->redis->pconnect($this->host, $this->port, $this->timeout);
$this->isAlive();
} catch (RedisException $e) {
$this->alive = false;
}

return $this;
}
}

13 changes: 12 additions & 1 deletion core/DB/NoSQL/RedisNoSQLList.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ final class RedisNoSQLList implements Listable
private $redis = null;
private $key = null;
private $position = null;
private $timeout = null;

public function __construct(Redis $redis, $key)
public function __construct(Redis $redis, $key, $timeout = null)
{
$this->redis = $redis;
$this->key = $key;
$this->timeout = $timeout;
}

/**
Expand All @@ -29,6 +31,9 @@ public function append($value)
{
$this->redis->rpush($this->key, $value);

if ($this->timeout)
$this->redis->setTimeout($this->key, $this->timeout);

return $this;
}

Expand All @@ -40,6 +45,9 @@ public function prepend($value)
{
$this->redis->lpush($this->key, $value);

if ($this->timeout)
$this->redis->setTimeout($this->key, $this->timeout);

return $this;
}

Expand Down Expand Up @@ -82,6 +90,9 @@ public function set($index, $value)
{
$this->redis->lset($this->key, $index, $value);

if ($this->timeout)
$this->redis->expire($this->key, $this->timeout);

return $this;
}

Expand Down