ThinkPHP 中使用Redis

环境.env

[app]
app_debug = "1"
app_trace = ""

[database]
database = ""
hostname = "127.0.0.1"
hostport = ""
password = ""
prefix = "ls_"
username = ""

[redis] 
hostname = "127.0.0.1"
hostport = "6379" 
password = "123456"
prefix = "redis_" 

[project]
env_name = ""
file_domain = "xxx.xxx.xxx.xxx" 

配置 config

  'complex', 
    'default'	=>	[
      'type'	=>	Env::get('cache.type','File'),
      // 全局缓存有效期(0为永久有效)
      'expire'=>  0, 
      // 缓存前缀
      'prefix'=>  'shop_',
       // 缓存目录
      'path'  =>  '',
    ],
    'redis'	=>	[
      'type'	=>	'redis',
      'host'	=> Env::get('redis.hostname'),
      'port'    => Env::get('redis.hostport'),
      'password' => Env::get('redis.password'),
      'expire'=>  0, 
      'prefix'=>  'redis_', // 缓存前缀
    ],    
    // 添加更多的缓存类型设置
];

Redis缓存处理类

rc = Cache::store('redis'); 
        $this->module = $module;
    }

    
    public function getkeys($key)
    {  
        return $this->rc->keys($key); 
    }

    public function setCache($key, $val)
    { 
        if($this->module) $key = $this->module.":".$key;
        return $this->rc->set($key, $val); 
    }

    public function getCache($key)
    { 
        if($this->module) $key = $this->module.":".$key;
        return $this->rc->get($key); 
    }  

    public function delete($key)
    { 
        if($this->module) $key = $this->module.":".$key;
        $prefix = Env::get('redis.prefix','');
        $key = $prefix.$key;
        return $this->rc->del($key);  
    }

    /**
     * 删除指定key的缓存
    *
    * 若 `$key===true` 则表示删除全部
    *
    *      // 支持下面的方式
    *      $this->delete('abc');
    *      $this->delete('abc', 'abc2');
    *      $this->delete(['abc', 'abc2']);
    *
    *      // 删除全部
    *      $this->delete(true);
    *      // 也可使用
    *      $this->delete_all();
    *
    * @param string|array|true $key
    */
    public function delCache($key)
    {   
        // $this->_connect(); 
        if (true === $key) {
            return $this->rc->flushAll();
        } else {
            if (is_array($key)) {
                $keys = $key;
            } else {
                $keys = func_get_args();
            }
            return $this->rc->del($keys);
        }
    } 
    
 
    public function hGetAllCache($key)
    { 
        if($this->module) $hash= $this->module.":".$key; 
        return $this->rc->hGetAll($hash); 
    } 
 

    // 判断hash表中字段是否存在,存在返回true,失败返回false
    public function hExistsCache($key, $field)
    {
        if($this->module) $hash= $this->module.":".$key;   
        return $this->rc->hExists($hash, $field); 
    } 

    public function hSetCache($key, $field, $val)
    {
        if($this->module) $hash= $this->module.":".$key;    
        return $this->rc->hSet($hash, $field, $val); 
    } 


    public function hGetCache($key, $field)
    {
        if($this->module) $hash= $this->module.":".$key;     
        return $this->rc->hGet($hash, $field); 
    } 

    public function hMgetCache($key, $fields)
    {
        // $fields = ['name', 'age']
        if($this->module) $hash= $this->module.":".$key;    
        return $this->rc->hMget($hash, $fields); 
    }

    public function hMsetCache($key, $entry)
    {
        // $entry = ['name' => 'jet', 'age' => 18]
        if($this->module) $hash= $this->module.":".$key;   
        return $this->rc->hMset($hash, $entry); 
    }
    
    
    public function hIncrByCache($key, $field, $add)
    { 
        if($this->module) $hash= $this->module.":".$key;   
        return $this->rc->hIncrBy($hash, $field, $add); 
    }

    public function hIncrByFloatCache($key, $field, $add)
    { 
        if($this->module) $hash= $this->module.":".$key;   
        return $this->rc->hIncrByFloat($hash, $field, $add); 
    }

}
?>

控制器中使用

服务器支持Redis服务
'; $redis = new RedisLogic(); $redis->setCache('key',12365478); echo '
key:',$redis->getCache('key'); echo $redis->delete('key'); } else{ echo '服务器不支持Redis服务'; } } }


 

使用redis的场景和对应示例代码icon-default.png?t=N7T8https://www.cnblogs.com/liuxinyustu/articles/13504257.html

你可能感兴趣的:(PHP,redis,数据库,缓存)