Skip to content

Commit

Permalink
Add Phalcon\Cache\Backend\Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamsxin committed Jul 1, 2014
1 parent 40e5cd2 commit 4a61db6
Show file tree
Hide file tree
Showing 5 changed files with 824 additions and 4 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ php:
services:
- mongodb
- memcached
- redis-server

before_install:
- ./unit-tests/ci/install_prereqs.sh
Expand Down
72 changes: 70 additions & 2 deletions phalcon/cache/backend/libmemcached.zep
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,13 @@ class Libmemcached extends \Phalcon\Cache\Backend implements \Phalcon\Cache\Back
let keys = memcache->get(specialKey);
if typeof keys == "array" {
for key in keys {
if !prefix || starts_with(key, prefix) {
return key;
if prefix && !starts_with(key, prefix) {
unset(keys[key]);
}
}
}

return keys;
}

/**
Expand Down Expand Up @@ -419,6 +421,72 @@ class Libmemcached extends \Phalcon\Cache\Backend implements \Phalcon\Cache\Back
return false;
}

/**
* Increment of given $keyName by $value
*
* @param string keyName
* @param long lifetime
* @return long
*/
public function increment(keyName=null, value=null)
{
var memcache, prefix, lastKey;

let memcache = this->_memcache;

if typeof memcache != "object" {
this->_connect();
let memcache = this->_memcache;
}

if !keyName {
let lastKey = this->_lastKey;
} else {
let prefix = this->_prefix;
let lastKey = prefix . keyName;
let this->_lastKey = lastKey;
}

if !value {
let value = 1;
}

return memcache->increment(lastKey, value);
}

/**
* Decrement of $keyName by given $value
*
* @param string keyName
* @param long value
* @return long
*/
public function decrement(keyName=null, value=null)
{
var memcache, prefix, lastKey;

let memcache = this->_memcache;

if typeof memcache != "object" {
this->_connect();
let memcache = this->_memcache;
}

if !keyName {
let lastKey = this->_lastKey;
} else {
let prefix = this->_prefix;
let lastKey = prefix . keyName;
let this->_lastKey = lastKey;
}

if !value {
let value = 1;
}

return memcache->decrement(lastKey, value);
}

/**
* Immediately invalidates all existing items.
*
Expand Down
107 changes: 105 additions & 2 deletions phalcon/cache/backend/memcache.zep
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,13 @@ class Memcache extends \Phalcon\Cache\Backend implements \Phalcon\Cache\BackendI
let keys = memcache->get(specialKey);
if typeof keys == "array" {
for key in keys {
if !prefix || starts_with(key, prefix) {
return key;
if prefix && !starts_with(key, prefix) {
unset(keys[key]);
}
}
}

return keys;
}

/**
Expand Down Expand Up @@ -361,4 +363,105 @@ class Memcache extends \Phalcon\Cache\Backend implements \Phalcon\Cache\BackendI
return false;
}

/**
* Increment of given $keyName by $value
*
* @param string keyName
* @param long lifetime
* @return long
*/
public function increment(keyName=null, value=null)
{
var memcache, prefix, lastKey;

let memcache = this->_memcache;

if typeof memcache != "object" {
this->_connect();
let memcache = this->_memcache;
}

if !keyName {
let lastKey = this->_lastKey;
} else {
let prefix = this->_prefix;
let lastKey = prefix . keyName;
let this->_lastKey = lastKey;
}

if !value {
let value = 1;
}

return memcache->increment(lastKey, value);
}

/**
* Decrement of $keyName by given $value
*
* @param string keyName
* @param long value
* @return long
*/
public function decrement(keyName=null, value=null)
{
var memcache, prefix, lastKey;

let memcache = this->_memcache;

if typeof memcache != "object" {
this->_connect();
let memcache = this->_memcache;
}

if !keyName {
let lastKey = this->_lastKey;
} else {
let prefix = this->_prefix;
let lastKey = prefix . keyName;
let this->_lastKey = lastKey;
}

if !value {
let value = 1;
}

return memcache->decrement(lastKey, value);
}

/**
* Immediately invalidates all existing items.
*
* @return boolean
*/
public function flush() -> boolean
{
var memcache, options, keys, specialKey, key;

let memcache = this->_memcache;

if typeof memcache != "object" {
this->_connect();
let memcache = this->_memcache;
}

let options = this->_options;

if !fetch specialKey, options["statsKey"] {
throw new \Phalcon\Cache\Exception("Unexpected inconsistency in options");
}

/**
* Get the key from memcached
*/
let keys = memcache->get(specialKey);
if typeof keys == "array" {
for key in keys {
memcache->delete(key);
}
memcache->set(specialKey, keys);
}
return true;
}

}
Loading

3 comments on commit 4a61db6

@mruz
Copy link
Contributor

@mruz mruz commented on 4a61db6 Jul 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dreamsxin, this commit duplicated the increment and decrement methods.

Zephir\CompilerException: Method 'increment' was defined more than one time in /home/abuild/rpmbuild/BUILD/php5-phalcon-zephir-2.0.0.6915f4d/phalcon/cache/backend/libmemcached.zep on line 463

@dreamsxin
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mruz Thank you, change it now.

@mruz
Copy link
Contributor

@mruz mruz commented on 4a61db6 Jul 14, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it, could you check whether I left correct #2607

Please sign in to comment.