欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

BAE使用Redis作为内存缓存

程序员文章站 2022-04-13 22:33:01
...

BAE内存缓存(phpRedis)

<?php
/*
 * Redis Cache
 */
class cache
{
	private $redis = null;//only this class
	private $mysql = null;

	public function __construct($host, $port, $username, $password, $database)
	{
		if (!class_exists('Redis'))
			return false;
		
		try
		{
			$this->redis = new Redis();
			$this->redis->connect($host, $port, '0.2');//timeout 200ms
			$this->redis->auth("{$username}-{$password}-{$database}");
		}
		catch (RedisException $error)
		{
			return false;
			//var_dump($error);
		}
	}


	public function get($key)
	{
		return $this->redis->get($key);
	}


	public function set($key, $value, $express=0)
	{
		if ($express)
			return $this->redis->setex($key, $express, $value);
		else
			return $this->redis->set($key, $value);
	}

	public function delete($key)
	{
		return $this->redis->delete($key);
	}

}
相关标签: php