Esempio n. 1
0
 /**
  * Give (if possible) an extra lifetime to the given cache id
  *
  * @param string $id cache id
  * @param int $extraLifetime
  * @return boolean true if ok
  */
 public function touch($id, $extraLifetime)
 {
     list($inf) = $this->_redis->hGet(self::PREFIX_KEY . $id, self::FIELD_INF);
     if ($inf === '0') {
         $expireAt = time() + $this->_redis->ttl(self::PREFIX_KEY . $id) + $extraLifetime;
         return (bool) $this->_redis->expireAt(self::PREFIX_KEY . $id, $expireAt);
     }
     return false;
 }
Esempio n. 2
0
 public function testHashes()
 {
     $this->assertEquals(1, $this->credis->hSet('hash', 'field1', 'foo'));
     $this->assertEquals(0, $this->credis->hSet('hash', 'field1', 'foo'));
     $this->assertEquals('foo', $this->credis->hGet('hash', 'field1'));
     $this->assertEquals(NULL, $this->credis->hGet('hash', 'x'));
     $this->assertTrue($this->credis->hMSet('hash', array('field2' => 'Hello', 'field3' => 'World')));
     $this->assertEquals(array('foo', 'Hello', FALSE), $this->credis->hMGet('hash', array('field1', 'field2', 'nilfield')));
     $this->assertEquals(array(), $this->credis->hGetAll('nohash'));
     $this->assertEquals(array('field1' => 'foo', 'field2' => 'Hello', 'field3' => 'World'), $this->credis->hGetAll('hash'));
 }
 /**
  * Update session
  *
  * @param string $sessionId
  * @param string $sessionData
  * @return boolean
  */
 public function write($sessionId, $sessionData)
 {
     if (!$this->_useRedis) {
         return parent::write($sessionId, $sessionData);
     }
     if ($this->_sessionWritten) {
         if ($this->_logLevel >= 7) {
             Mage::log(sprintf("%s: Repeated session write detected; skipping for ID %s", $this->_getPid(), $sessionId), Zend_Log::DEBUG, self::LOG_FILE);
         }
         return TRUE;
     }
     $this->_sessionWritten = TRUE;
     if ($this->_logLevel >= 7) {
         Mage::log(sprintf("%s: Attempting write to ID %s", $this->_getPid(), $sessionId), Zend_Log::DEBUG, self::LOG_FILE);
         // reset timer
         $this->_timeStart = microtime(true);
     }
     // Do not overwrite the session if it is locked by another pid
     try {
         if ($this->_dbNum) {
             $this->_redis->select($this->_dbNum);
         }
         // Prevent conflicts with other connections?
         $pid = $this->_redis->hGet('sess_' . $sessionId, 'pid');
         // PHP Fatal errors cause self::SESSION_PREFIX to not work..
         if (!$pid || $pid == $this->_getPid()) {
             if ($this->_logLevel >= 7) {
                 Mage::log(sprintf("%s: Write lock obtained on ID %s", $this->_getPid(), $sessionId), Zend_Log::DEBUG, self::LOG_FILE);
             }
             $this->_writeRawSession($sessionId, $sessionData, $this->getLifeTime());
             if ($this->_logLevel >= 7) {
                 Mage::log(sprintf("%s: Data written to ID %s after %.5f seconds", $this->_getPid(), $sessionId, microtime(true) - $this->_timeStart), Zend_Log::DEBUG, self::LOG_FILE);
             }
         } else {
             if ($this->_logLevel >= 4) {
                 if ($this->_hasLock) {
                     Mage::log(sprintf("%s: Unable to write session after %.5f seconds, another process took the lock for ID %s", $this->_getPid(), microtime(true) - $this->_timeStart, $sessionId), Zend_Log::WARN, self::LOG_FILE);
                 } else {
                     Mage::log(sprintf("%s: Unable to write session after %.5f seconds, unable to acquire lock on ID %s", $this->_getPid(), microtime(true) - $this->_timeStart, $sessionId), Zend_Log::WARN, self::LOG_FILE);
                 }
             }
         }
     } catch (Exception $e) {
         if (class_exists('Mage', false)) {
             Mage::logException($e);
         } else {
             error_log("{$e}");
         }
         return FALSE;
     }
     return TRUE;
 }
Esempio n. 4
0
 public function testHashes()
 {
     $this->assertEquals(1, $this->credis->hSet('hash', 'field1', 'foo'));
     $this->assertEquals(0, $this->credis->hSet('hash', 'field1', 'foo'));
     $this->assertEquals('foo', $this->credis->hGet('hash', 'field1'));
     $this->assertEquals(NULL, $this->credis->hGet('hash', 'x'));
     $this->assertTrue($this->credis->hMSet('hash', array('field2' => 'Hello', 'field3' => 'World')));
     $this->assertEquals(array('foo', 'Hello', FALSE), $this->credis->hMGet('hash', array('field1', 'field2', 'nilfield')));
     $this->assertEquals(array(), $this->credis->hGetAll('nohash'));
     $this->assertEquals(array('field1' => 'foo', 'field2' => 'Hello', 'field3' => 'World'), $this->credis->hGetAll('hash'));
     // Test long hash values
     $longString = str_repeat(md5('asd'), 4096);
     // 128k (redis.h REDIS_INLINE_MAX_SIZE = 64k)
     $this->assertEquals(1, $this->credis->hMSet('long_hash', array('count' => 1, 'data' => $longString)), 'Set long hash value');
     $this->assertEquals($longString, $this->credis->hGet('long_hash', 'data'), 'Get long hash value');
 }
 public function _load($session_id)
 {
     $host = (string) (Mage::getConfig()->getNode(self::XML_PATH_HOST) ?: '127.0.0.1');
     $port = (int) (Mage::getConfig()->getNode(self::XML_PATH_PORT) ?: '6379');
     $pass = (string) (Mage::getConfig()->getNode(self::XML_PATH_PASS) ?: '');
     $timeout = (double) (Mage::getConfig()->getNode(self::XML_PATH_TIMEOUT) ?: '2.5');
     $_redis = new Credis_Client($host, $port, $timeout);
     if (!empty($pass)) {
         $_redis->auth($pass);
     }
     $_redis->connect();
     // connect to redis
     // replace sess_session_id with session id you want to read.
     $sessionData = $_redis->hGet('sess_' . $session_id, 'data');
     // only data field is relevant to us, uncompress the data
     return $sessionData ? $this->_decodeData($sessionData) : false;
 }
Esempio n. 6
0
 /**
  * Update session
  *
  * @param string $sessionId
  * @param string $sessionData
  * @return boolean
  */
 public function write($sessionId, $sessionData)
 {
     $this->profilerStart(__METHOD__);
     if ($this->_sessionWritten) {
         if ($this->_logLevel >= \Zend_Log::DEBUG) {
             $this->_log(sprintf("Repeated session write detected; skipping for ID %s", $sessionId));
         }
         $this->profilerStop(__METHOD__);
         return TRUE;
     }
     $this->_sessionWritten = TRUE;
     if ($this->_logLevel >= \Zend_Log::WARN) {
         $timeStart = microtime(true);
     }
     // Do not overwrite the session if it is locked by another pid
     try {
         if ($this->_dbNum) {
             $this->_redis->select($this->_dbNum);
         }
         // Prevent conflicts with other connections?
         if (!$this->_useLocking || (!($pid = $this->_redis->hGet('sess_' . $sessionId, 'pid')) || $pid == $this->_getPid())) {
             $this->_writeRawSession($sessionId, $sessionData, $this->getLifeTime());
             if ($this->_logLevel >= \Zend_Log::DEBUG) {
                 $this->_log(sprintf("Data written to ID %s in %.5f seconds", $sessionId, microtime(true) - $timeStart));
             }
         } else {
             if ($this->_logLevel >= \Zend_Log::WARN) {
                 if ($this->_hasLock) {
                     $this->_log(sprintf("Did not write session for ID %s: another process took the lock.", $sessionId), \Zend_Log::WARN);
                 } else {
                     $this->_log(sprintf("Did not write session for ID %s: unable to acquire lock.", $sessionId), \Zend_Log::WARN);
                 }
             }
         }
     } catch (\Exception $e) {
         $this->logger->logException($e);
         $this->profilerStop(__METHOD__);
         return FALSE;
     }
     $this->profilerStop(__METHOD__);
     return TRUE;
 }