zoukankan      html  css  js  c++  java
  • redis类与用法

    <?php
    namespace appcommonmodel;

    class Cache
    {

    public $redis = null;

    public function __construct() {
    if (is_null($this->redis)) {
    $this->redis = new Redis();
    $this->redis->connect('127.0.0.1', '6379');
    }
    }
    //获取对应缓存
    //subkey = 's'
    public function get_obj_cache($key = '', $subkey = '')
    {

    $data = false;
    $key = (string)$key;
    $subkey = (string)$subkey;

    // 检查变量
    if (!is_not_empty_string($key))
    {
    return $data;
    }
    // 读取数据
    if (is_not_empty_string($subkey) && $subkey == 's')
    {
    $data = $this->redis->smembers($key);
    if(is_not_empty_array($data)) return $data;

    return false;
    }
    else
    {
    $data = $this->redis->get($key);
    }

    return $data;
    }


    //删除对应缓存
    public function cache_del ( $key = '') {
    $state = false;
    // 检查变量
    if ( !is_not_empty_string( $key ))
    {
    return $state;
    }
    // 删除数据
    $this->redis->del($key);
    $state = true;
    return $state;
    }

    //更新对应缓存 key_val
    public function cache_item($key = '', $value = '', $cache_time = 0)
    {
    $state = false;
    // 检查参数
    if ( !is_not_empty_string( $key ))
    {
    return $state;
    }
    // 存储数据
    $this->redis->set($key, $value);
    // 设置过期时间
    if ($cache_time > 0) {
    $this->redis->expire($key, (int)$cache_time);
    }

    $state = true;

    return $state;
    }
    //更新对应集合key
    public function cache_Sitem ($key = '', $value = '') {
    $state = false;
    // 检查参数
    if (!is_not_empty_string($key) || empty($value))
    {
    return $state;
    }
    // 存储数据
    $this->redis->sadd($key, $value);
    $state = true;
    return $state;
    }
    //移除集合元素
    public function ceche_Sremove ($key = '', $value = '') {
    $state = false;
    $key = (string)$key;
    // 检查参数
    if (!is_not_empty_string($key))
    {
    return $state;
    }
    // 移除元素
    $this->redis->srem($key, $value);
    $state = true;
    return $state;
    }

    }

    共用的model类使用

    <?php
    namespace appindexmodel;
    use thinkModel;
    use thinkRequest;
    use thinkDb;
    use thinkCookie;
    use appcommonmodelCache;
    class Suv extends Model
    {
    private $Dk_User_Info_Cache_Key = 'Dk_User_Info_Cache_Uid_%d'; //获取查询的id
    private $Cache_time = 604800; //默认缓存时间

    public function suibian($uid = 0){
    $data = [];
    $Cache_obj = new Cache();
    $cachekey = sprintf($this->Dk_User_Info_Cache_Key, $uid);//Dk_User_Info_Cache_Uid_1

    $data = unserialize($Cache_obj -> get_obj_cache($cachekey));//读取redis缓存,反序列化

    if(false === $data){
    //echo 'kong';
    $UserInfo = Db::table('mb_user')->where('UserId','1')->find();
    if(!empty($UserInfo)){
    $Cache_obj->cache_item($cachekey, serialize( $UserInfo), $this->Cache_time);//序列化后存到redis中
    }
    $data = $UserInfo;
    }
    return $data;
    }

    }

    控制器使用公共model类

    $AAA = new Suv();
    $res = $AAA->suibian(1);

  • 相关阅读:
    终于等到你---订餐系统之负载均衡(nginx+memcached+ftp上传图片+iis)
    订餐系统之同步饿了么商家订单
    订餐系统之同步口碑外卖商家菜单与点点送订单
    基于SuperSocket的IIS主动推送消息给android客户端
    基于mina框架的GPS设备与服务器之间的交互
    订餐系统之微信支付,踩了官方demo的坑
    订餐系统之自动确认淘点点订单
    订餐系统之Excel批量导入
    移除首页->重回首页
    订餐系统之获取淘宝外卖订单
  • 原文地址:https://www.cnblogs.com/yszr/p/8279874.html
Copyright © 2011-2022 走看看