<?php
class Red {
static private $redis = NULL;
private $_red = NULL;
private $_return_data = NULL;
static public function create() {
if(self::$redis) {
return Red::$redis;
}
self::$redis = new self;
return self::$redis;
}
public function __call($func, $params) {
if ($func == 'multi') {
$this->_return_data = $this->_red->multi($params[0]);
} else {
$this->_return_data = call_user_func_array(array(&$this->_red, $func), $params);
}
return $this->_return_data;
}
private function __construct() {
$this->_red = new Redis();
$this->_red->connect(C("REDIS_HOST"),C("REDIS_PORT"));
$this->_red->select(C('REDIS_DB') ?: 0);
return Red::$redis;
}
public function ttl($key)
{
$new_key = C('REDIS_PREV') . $key;
return $this->_red->ttl($new_key);
}
public function del($key)
{
$new_key = C('REDIS_PREV') . $key;
return $this->_red->del($new_key);
}
public function delete($key)
{
$new_key = C('REDIS_PREV') . $key;
return $this->_red->delete($new_key);
}
public function rename($key1, $key2)
{
$new_key1 = C('REDIS_PREV') . $key1;
$new_key2 = C('REDIS_PREV') . $key2;
return $this->_red->rename($new_key1, $new_key2);
}
public function exists($key)
{
$new_key = C('REDIS_PREV') . $key;
return $this->_red->exists($new_key);
}
public function setex($key, $time ,$value)
{
$new_key = C('REDIS_PREV') . $key;
return $this->_red->setex($new_key, $time, $value);
}
public function set($key, $value , $time = '')
{
$new_key = C('REDIS_PREV') . $key;
if (is_numeric($time) && $time > 0) {
return $this->_red->set($new_key, $value , $time);
} else {
return $this->_red->set($new_key, $value);
}
}
public function get($key)
{
$new_key = C('REDIS_PREV') . $key;
$a = $this->_red->get($new_key);
return $a;
}
}
<?php
class CartModel
{
protected $redis;
public function __construct()
{
vendor('Redis.Red');
$this->redis = Red::create();
}
public function add($key, $id, $count = 1)
{
$key = C('REDIS_PREV') . $key;
return $this->redis->hIncrBy($key, $id, $count);
}
public function addEx($key, array $data)
{
$key = C('REDIS_PREV') . $key;
$r = $this->redis->multi(Redis::PIPELINE);
foreach ($data as $k => $v) {
$r = $r->hIncrBy($key, $k, $v);
}
return $this->redis->exec();
}
public function delete($key, $id)
{
$key = C('REDIS_PREV') . $key;
$id = $this->redis->hdel($key, $id);
return $id;
}
public function deleteEx($key, $ids)
{
$key = C('REDIS_PREV') . $key;
foreach ($ids as $k => $v) {
$this->redis->hdel($key, $v);
}
return true;
}
public function exists($key, $id)
{
$key = C('REDIS_PREV') . $key;
return $this->redis->hExists($key, $id);
}
public function deleteAll($key)
{
$key = C('REDIS_PREV') . $key;
$log = [];
$log['token'] = $key;
$log['time'] = date('Y-m-d H:i:s');
$log['type'] = 'cart_model_deleteAll';
file_put_contents('/tmp/yh.del.token.log', print_r($log, true), FILE_APPEND);
return $this->redis->del($key);
}
public function count($key)
{
$key = C('REDIS_PREV') . $key;
return $this->redis->hLen($key);
}
public function merge($guest, $uid)
{
if (!$this->hasGuestCart($guest)) {
return true;
}
if (!$this->hasUserCart($uid)) {
$this->redis->rename(C('GUEST.CART') . $guest, C('USER.CART') . $uid);
return true;
}
$guestCart = $this->redis->hgetall(C('GUEST.CART') . $guest);
$userCart = $this->redis->hgetall(C('USER.CART') . $uid);
foreach ($guestCart as $k => $v) {
if (isset($userCart[$k])) {
$userCart[$k] += $v;
} else {
$userCart[$k] = $v;
}
}
$this->deleteAll(C('GUEST.CART') . $guest);
$this->deleteAll(C('USER.CART') . $uid);
return $this->redis->hMset(C('USER.CART') . $uid, $userCart);
}
public function hasGuestCart($guest)
{
return $this->count(C('GUEST.CART').$guest);
}
public function hasUserCart($uid)
{
return $this->count(C('USER.CART').$uid);
}
public function setCount($key, $id, $count)
{
$key = C('REDIS_PREV') . $key;
$status = $this->redis->hset($key, $id, $count);
if ($status == -1) {
return false;
}
return true;
}
public function getCount($key, $id)
{
$key = C('REDIS_PREV') . $key;
return $this->redis->hget($key, $id);
}
public function get($key)
{
$key = C('REDIS_PREV') . $key;
return $this->redis->hgetall($key);
}
}
//end
相信坚持的力量,日复一日的习惯.