예제 #1
1
 public function set($index, $value)
 {
     $this->redis->lset($this->key, $index, $value);
     if ($this->timeout) {
         $this->redis->expire($this->key, $this->timeout);
     }
     return $this;
 }
예제 #2
1
파일: Cache.php 프로젝트: pizar/gaia
 public function incrBy($chiave, $valore = 1, $scadenza = Cache::SCADENZA_DEFAULT)
 {
     $x = parent::incrBy($chiave, $valore);
     if ($scadenza) {
         parent::expire($chiave, $scadenza);
     }
     return $x;
 }
예제 #3
0
파일: Redis.php 프로젝트: warmans/dlock
 /**
  * {@inheritdoc}
  */
 public function acquireLock($lockId)
 {
     if ($this->conn->setnx($lockId, 1)) {
         $this->conn->expire($lockId, $this->lockTtl);
         return true;
     }
     return false;
 }
예제 #4
0
 /**
  * Store a value for a given key in the cache.
  *
  * This method does not check if there is a value available for the given key, may cause unexpected behavior if not.
  * Use has() to prevent this issue.
  *
  * @param string $key
  * @param array  $tags
  * @param mixed  $data
  */
 public function set($key, array $tags, $data)
 {
     $key = $this->redis->prefix($key);
     $this->redis->set($key, $this->encoder->encode($data));
     if (is_int($this->expirationTime) && $this->expirationTime > 0) {
         $this->redis->expire($key, $this->expirationTime);
     }
     foreach ($tags as $tag) {
         $this->redis->sadd($this->redis->prefix($tag), $key);
     }
 }
예제 #5
0
 public function setTTL($key, $ttl)
 {
     if (!$this->isConnected()) {
         return false;
     }
     if (!$this->exists($key)) {
         return false;
     }
     if (!is_numeric($ttl)) {
         throw new InvalidTtlException($ttl);
     }
     return $this->redis->expire($key, (int) $ttl);
 }
예제 #6
0
 /**
  * Set a value identified by $key and with an optional $ttl.
  *
  * @param string $key
  * @param mixed  $value
  * @param int    $ttl
  *
  * @return $this
  */
 public function set($key, $value, $ttl = 0)
 {
     $ttl = $this->fromDefaultTtl($ttl);
     if ($ttl >= 0) {
         if ($this->isAvailable()) {
             $this->redis->set($key, $this->storageDataStructure($value));
             if ($ttl > 0) {
                 $this->redis->expire($key, $ttl);
             }
         }
         $this->setChain($key, $value, $ttl);
     }
     return $this;
 }
 /**
  * @param string $sessionId
  */
 private function lockSession($sessionId)
 {
     $attempts = 1000000 / $this->spinLockWait * $this->lockMaxWait;
     $this->lockKey = $sessionId . '.lock';
     for ($i = 0; $i < $attempts; $i++) {
         $success = $this->redis->setnx($this->prefix . $this->lockKey, '1');
         if ($success) {
             $this->locked = true;
             $this->redis->expire($this->prefix . $this->lockKey, $this->lockMaxWait + 1);
             return true;
         }
         usleep($this->spinLockWait);
     }
     return false;
 }
예제 #8
0
파일: Wk_Redis.php 프로젝트: telander/waka
 /**
  * @param string $key
  * @param int $ttl
  * @return bool
  */
 public function expire($key, $ttl)
 {
     $this->_useCnt++;
     return $this->_wrapBoolReturn(function () use($key, $ttl) {
         return parent::expire($key, $ttl);
     });
 }
예제 #9
0
 /**
  * Appends data to an existing item on the Redis server.
  *
  * @param  string $key
  * @param  string $value
  * @param  int    $expiration
  * @return bool
  */
 protected function appendValue($key, $value, $expiration = 0)
 {
     if ($this->redis->exists($key)) {
         $this->redis->append($key, $value);
         return $this->redis->expire($key, $expiration);
     }
     return $this->redis->setex($key, $expiration, $value);
 }
예제 #10
0
 protected function setCached(Bookmark $bookmark, $redisKey)
 {
     if ($this->redis !== null) {
         $this->redis->hSet($redisKey, $bookmark->url, 1);
         if ($this->redis->ttl($redisKey) < 0) {
             $this->redis->expire($redisKey, 4 * 7 * 24 * 60 * 60);
         }
     }
 }
예제 #11
0
 /**
  * Set an item in the cache
  *
  * @param string $key
  * @param mixed $value
  * @param int|\DateInterval|\DateTime $validity Number of seconds this is valid for (if int)
  * @return void
  */
 function set($key, $value, $validity = null)
 {
     $this->connection->set($key, $value);
     if ($validity instanceof \DateInterval) {
         $validity = (new \DateTimeImmutable())->add($validity);
     }
     if ($validity instanceof \DateTimeInterface) {
         $this->connection->expireat($key, $validity->getTimestamp());
     } else {
         if (is_numeric($validity)) {
             $this->connection->expire($key, $validity);
         } else {
             if ($validity != null) {
                 throw new \UnexpectedValueException("Unexpected validity");
             }
         }
     }
 }
예제 #12
0
파일: Redis.php 프로젝트: janeklb/moodle
 /**
  * Set the last modified time to the current time
  *
  * @return bool Success status
  */
 public function touch()
 {
     $data = $this->cache->get($this->name);
     if ($data !== false) {
         $return = $this->cache->set($this->name, $data);
         if ($this->options['expire']) {
             return $this->cache->expire($this->name, $this->ttl);
         }
         return $return;
     }
     return false;
 }
function test4_set2()
{
    $cache = new Redis();
    $cache->pconnect('127.0.0.1', 7001);
    $t1 = microtime(1);
    for ($i = 0; $i < 10000; $i++) {
        $cache->set('qwe' . $i, 'hello' . $i);
        $cache->expire('qwe' . $i, 1800);
    }
    $t2 = microtime(1);
    var_dump($t2 - $t1);
    echo "\n";
}
예제 #14
0
 public function testPersist()
 {
     $this->redis->set('x', 'y');
     $this->redis->expire('x', 100);
     $this->assertTrue(TRUE === $this->redis->persist('x'));
     // true if there is a timeout
     $this->assertTrue(-1 === $this->redis->ttl('x'));
     // -1: timeout has been removed.
     $this->assertTrue(FALSE === $this->redis->persist('x'));
     // false if there is no timeout
     $this->redis->del('x');
     $this->assertTrue(FALSE === $this->redis->persist('x'));
     // false if the key doesn’t exist.
 }
예제 #15
0
 /**
  * Write data to the session.
  *
  * @param string  $id
  * @param mixed   $data
  * @return bool
  */
 public function write($id, $data)
 {
     // This might be the first set for this session key, so we have to check if it's already exists in redis
     if ($this->_id !== $id) {
         // Perform getset to check if session already exists in redis
         if ($this->_redis->getSet($this->_buildKey($id), $data) === false) {
             // session doesn't exists in redis, so we have to set expiration
             $this->_redis->expire($this->_buildKey($id), $this->_config['lifetime']);
         }
         // Backup id, so we'll know that expiration has already been set, we'll avoid to perform getset on the next write
         $this->_id = $id;
     } else {
         // This is not the first write in script execution, just update value
         $this->_redis->set($this->_buildKey($id), $data);
     }
     return true;
 }
예제 #16
0
 /**
  * 设置一个过期时间
  * @param long $expire 要过期的时间,秒或毫秒
  * @param bool $isMs 为TRUE时表示$expire是ms(1s=1000ms)
  */
 public function expire($expire, $isMs = FALSE)
 {
     try {
         if ($isMs) {
             return $this->rd->pexpire($this->k, $expire);
         } else {
             return $this->rd->expire($this->k, $expire);
         }
     } catch (\RedisException $e) {
         if ($this->reconnRedis()) {
             if ($isMs) {
                 return $this->rd->pexpire($this->k, $expire);
             } else {
                 return $this->rd->expire($this->k, $expire);
             }
         }
     }
     return FALSE;
 }
 public function _write_cache($output)
 {
     $CI =& get_instance();
     $path = $CI->config->item('cache_path');
     $cache_path = $path === '' ? APPPATH . 'cache/' : $path;
     $uri = $CI->config->item('base_url') . $CI->config->item('index_page') . $CI->uri->uri_string();
     if ($CI->config->item('cache_query_string') && !empty($_SERVER['QUERY_STRING'])) {
         $uri .= '?' . $_SERVER['QUERY_STRING'];
     }
     $cache_path .= md5($uri);
     $redis = new Redis();
     $host = $CI->config->item("redis_host");
     $port = $CI->config->item("redis_port");
     $redis->connect($host, $port);
     if (!$redis->ping()) {
         log_message('error', "Unable to ping to redis {$host}:{$port}");
         return false;
     }
     if ($this->_compress_output === TRUE) {
         $output = gzencode($output);
         if ($this->get_header('content-type') === NULL) {
             $this->set_content_type($this->mime_type);
         }
     }
     $expire = time() + $this->cache_expiration;
     $cache_info = serialize(array('last_modified' => time(), 'expire' => $expire, 'headers' => $this->headers));
     $output = $cache_info . 'ENDCI--->' . $output;
     try {
         $redis->set($cache_path, $output);
         $redis->expire($cache_path, $this->cache_expiration);
         $this->set_cache_header($_SERVER['REQUEST_TIME'], $expire);
     } catch (RedisException $e) {
         log_message('error', "Unable to set cache key");
         return false;
     }
 }
예제 #18
0
	public function save(){
		Redis::hmset("Source::{$this->id}",
			'features', json_encode($this->features),
			'type', json_encode($this->type),
			'origin', json_encode($this->origin)
		);
		
		if($this->isUpload()){
			Redis::expire("Source::{$this->id}", UPLOAD_EXPIRE_TIME);
		}
	}
<?php

ob_start();
header("Content-Type:application/json; charset=UTF-8");
include "../ali-api/TopSdk.php";
date_default_timezone_set('Asia/Shanghai');
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
/**
 * Created by PhpStorm.
 * User: dong_rui
 * Date: 2016-01-29
 * Time: 14:56
 */
session_start();
$ran_num = rand(1000, 9999);
$redis->set(session_id() . '_verify_code', $ran_num);
$redis->expire(session_id() . '_verify_code', 60);
$c = new TopClient();
$c->appkey = '23305908';
$c->secretKey = '9655a0b98c6f8f3bfaf9a40cad1b5696';
$req = new AlibabaAliqinFcSmsNumSendRequest();
$req->setExtend("123456");
$req->setSmsType("normal");
$req->setSmsFreeSignName("百变女人");
$req->setSmsParam("{\"code\":\"{$ran_num}\", \"product\":\"Lady荟\"}");
$req->setRecNum($_POST["phone_number"]);
$req->setSmsTemplateCode("SMS_5000839");
$resp = $c->execute($req);
$_SESSION["mobile"] = $_POST["phone_number"];
echo json_encode($resp);
예제 #20
0
 /**
  * set a value expire time
  *
  * @param	string	$key Cache ID
  * @param	int	$seconds expire seconds
  * @return	mixed	New value on success or FALSE on failure
  */
 public function expire($key, $seconds)
 {
     return $this->_redis->expire($key, $seconds);
 }
예제 #21
0
 /**
  * @param string $key
  * @param int    $ttl
  */
 public function expire($key, $ttl)
 {
     $this->_redis->expire($key, $ttl);
 }
예제 #22
0
 /**
  * 设置过期时间
  * @param $queue
  * @param $ttl
  * @return bool
  */
 public function setExpire($queue, $ttl)
 {
     return $this->redis->expire($queue, $ttl);
 }
예제 #23
0
 /**
  * 将key->value写入hash表中
  * @param $hash string 哈希表名
  * @param $data array 要写入的数据 array('key'=>'value')
  * @param $time longint 过期时间(S)  默认值为0-不设置过期时间
  */
 public static function hashSet($hash, $data, $time = 0)
 {
     $redis = new \Redis();
     $redis->connect(self::_HOST, self::_PORT);
     $return = null;
     if (is_array($data) && !empty($data)) {
         $return = $redis->hMset($hash, $data);
         if ($time && is_numeric($time)) {
             $redis->expire($hash, $time);
         }
     }
     $redis->close();
     $redis = null;
     return $return;
 }
예제 #24
0
 public function setRedisData($key, $val)
 {
     if (class_exists('Redis')) {
         try {
             $redis = new Redis();
             $redis->connect($this->getRedisIpPort());
             $redis->set($key, $val);
             $redis->expire($key, $this->getTimeLiveCache());
         } catch (RedisException $e) {
             return false;
         }
         return true;
     }
     return false;
 }
예제 #25
0
if ($redis->exists($keyname)) {
    $status = $redis->hget($keyname, 'status');
    $cookieexist = $redis->hget($keyname, 'cookie');
    if ($status == 'processing') {
        echo "<script>alert('正在为你领取银瓜子~');history.go(-1);</script>";
        exit;
    } elseif ($status == 'processed') {
        echo "<script>alert('今天的银瓜子已经领完了,明天再来吧~');history.go(-1);</script>";
        exit;
    } elseif ($status == 'problem') {
        if ($cookieexist == $cookie) {
            echo "<script>alert('Cookie数据有问题,请尝试重新登录获取后再提交!');history.go(-1);</script>";
            exit;
        } else {
            echo "<script>alert('成功更新Cookie数据!');</script>";
        }
    } else {
        echo "<script>alert('你的任务正在队列中,稍后再看看吧~');history.go(-1);</script>";
        exit;
    }
}
$outtime = strtotime("tomorrow") - time();
if ($outtime <= 3600) {
    echo "<script>alert('今天太晚了,明天再来吧~');history.go(-1);</script>";
    exit;
}
$redis->hset($keyname, 'cookie', $cookie);
$redis->hset($keyname, 'status', 'queuing');
$redis->expire($keyname, $outtime);
echo "<script>alert('你的任务已经加入队列中,下一个小时开始领取!');history.go(-1);</script>";
exit;
예제 #26
0
	public function save(){
		Redis::hmset("Repository::{$this->id}", 'name', json_encode($this->name), 'url', json_encode($this->url));
		
		// Can't recommend an uploaded file to a user, so don't bother inserting
		// it into the ranking sets, and go ahead and remove the reference to it
		// at some point in the near future
		if($this->isUpload()){
			Redis::expire("Repository::{$this->id}", UPLOAD_EXPIRE_TIME);
			return;
		}
		
		// If it is a repository, we want to insert it into the ranking sets	
		foreach($this->features() as $type => $attributes){
			foreach($attributes as $attribute => $value){
				Redis::zadd("Match::{$type}::{$attribute}", $value, $this->id);
			}
		}
	}
예제 #27
0
 /**
  * @inheritdoc
  */
 public function finish($id)
 {
     $this->redis->expire($id, $this->expireTime);
 }
예제 #28
0
파일: getimg.php 프로젝트: koala87/backup
if ($use_cache) {
    $path = dirname(__FILE__) . trim($_GET['path']);
    if (file_exists($path)) {
        $redis = new Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->select(10);
        //断点续传206
        http_response_code(206);
        header('Content-type: image/jpeg');
        if ($redis->exists($path)) {
            echo $redis->get($path);
        } else {
            $img = file_get_contents($path);
            echo $img;
            $redis->set($path, $img);
            $redis->expire($path, 86400 * 30);
        }
    } else {
        /*
        $fileName = date('Y-m-d').'img.log';
        $text = $path.PHP_EOF;
        if($fp = fopen($fileName, 'a')) {
        	if(@fwrite($fp, $text)) {
        		@fclose($fp);
        	} else {
        		@fclose($fp);
        	}
        }
        */
    }
} else {
예제 #29
-1
파일: redis.php 프로젝트: xiehaowei/php
<?php

/**
 * Created by PhpStorm.
 * User: xiehaowei
 * Date: 2016/10/19
 * Time: 下午4:32
 */
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
$key = 'test-expire-key';
$redis->expire($key, 60);
//使用秒为单位
$redis->pExpire($key, 60000);
//使用毫秒作为单位
$redis->expireAt($key, 1476868380);
//使用Unix timestamp,指定时间过期
$redis->pExpireAt($key, 1476868380000.0);
//使用Unix timestamp在指定时间过期,区别是毫秒作为单位
$redis->persist($key);
//移除给定key的生存时间
$redis->ttl($key);
//返回key剩余的过期时间,使用秒为单位
$redis->pttl($key);
//返回key剩余的过期时间,使用毫秒作为单位
$redis->watch($key2);
$ret = $redis->multi(Redis::MULTI)->get($key)->incr($key1)->del($key2)->exec();
$pip->incr('a');
$pip->get('test-num2');
$pip->incr('b');
$pip->del('a');
예제 #30
-1
파일: CRedis.php 프로젝트: nbaiwan/yav
 public function expire($key, $timeout)
 {
     return parent::expire($this->generateUniqueKey($key), $timeout);
 }