key要統一管理起來,便於後續的閱讀以及擴展php
<?php namespace libs; /** * 緩存key映射類:緩存KEY要統一配置,便於後期批量更改和管理 * 注意其命名規則: 項目名:模塊名:名稱:類型 tkmall:mem:uid:hash * Class CacheKeyMap * @package libs */ class CacheKeyMap { public static $prefix = 'tkmall:'; /** * 基於會員uid的hash,管理會員資料 * @param $uid * @param int $prefix * @return string */ public static function memberUidHash($uid,$prefix=0) { if($prefix){ // 用於keys,scan等命令 return self::$prefix . 'mem:' . $uid .':*'; } return self::$prefix . 'mem:' . $uid .':hash'; } }
<?php namespace libs; use think\cache\driver\Redis as tpRedis; /** * 定製化的redis * Class Redis * @package libs */ class Redis extends tpRedis{ protected static $_instance = null; /** * 獲取單例redis對象,通常用此方法實例化 * @return Redis|null */ public static function getInstance() { if(!is_null(self::$_instance)){ return self::$_instance; } self::$_instance = new self(); return self::$_instance; } /** * 架構函數 * Redis constructor. */ public function __construct() { $options = config('cache.redis'); $options['prefix'] = CacheKeyMap::$prefix; parent::__construct($options); } /** * 覆寫,實際的緩存標識以CacheKeyMap來管理 * @access protected * @param string $name 緩存名 * @return string */ protected function getCacheKey($name) { return $name; } /** * redis排重鎖 * @param $key * @param $expires * @param int $value * @return mixed */ public function redisLock($key, $expires, $value = 1) { //在key不存在時,添加key並$expires秒過時 return $this->handler()->set($key, $value, ['nx', 'ex' => $expires]); } /** * 調用緩存類型本身的高級方法 * @param $method * @param $args * @return mixed|void * @throws \Exception */ public function __call($method,$args){ if(method_exists($this->handler, $method)){ return call_user_func_array(array($this->handler,$method), $args); }else{ exception(__CLASS__.':'.$method.'不存在'); return; } } }
app/provider.php
redis
// 應用容器綁定定義 return [ 'redis' => 'libs\Redis' ];
$redis = $this->app['redis']; $redis->hMSet(CacheKeyMap::memberUidHash($uid), ['name' => 'Joe', 'salary' => 2000]);