本文主要是介绍tp5拓展redis操作模型RedisModel,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
php中的redis操作接口
thinkphp5
<?php
namespace app\common\Model;use think\Db;
use think\Model;class RedisModel extends Model{protected static $redis = null;protected static function init(){self::$redis = new \Redis();$config = config( 'redis.' );self::$redis->connect( $config['host'], $config['port'] );}/*********************hash操作 start***********************//*** 根据查询条件,将查询结果写入redis缓存* @author liwuming* @date 2019-04-26*/public function mysql_to_hash($key_name = '', $table = '', $fields = '*', $where = [], $order = [], $field = 'id'){$result = 0;if(!empty( $table ) && !empty( $key_name )){$tables = is_object( $table ) ? $table : Db::name( $table );$data_list = $tables->field( $fields )->where( $where )->order( $order )->select()->toArray();if(empty( $data_list ))return $result;if(!empty( $data_list ) && array_key_exists( $field, $data_list[0] )){$key_name .= config( '__LXMALL_ID__' );$result = 0;foreach( $data_list as $data_info ){$tmp = self::$redis->hset( $key_name, $data_info[$field], json_encode( $data_info ) );if($tmp){$result ++;}}}}return $result;}/*** 将数组以hash的方式存入缓存* @author liwuming* @date 2019-04-27*/public function array_to_hash($key_name = '', $data_list = [], $field = 'id'){$result = 0;if(!empty( $key_name ) && !empty( $data_list )){$key_name .= config( '__LXMALL_ID__' );if(!empty( $field )){$data_first = reset( $data_list );if(!empty( $data_first[$field] )){foreach( $data_list as $data ){$tmp = self::$redis->hset( $key_name, $data[$field], json_encode( $data ) );if($tmp){$result ++;}}}}else{foreach( $data_list as $key => $data ){$data = is_array( $data ) ? json_encode( $data ) : $data;$tmp = self::$redis->hset( $key_name, $key, $data );if($tmp){$result ++;}}}}return $result;}/*** 将单条记录以hash的方式写入缓存* @author liwuming* @date 2019-04-27*/public function set_redis_hash($key_name = '', $key = '', $data = ''){if(!empty( $key_name ) && !empty( $key ) && !empty( $data )){$key_name .= config( '__LXMALL_ID__' );$data = is_array( $data ) ? json_encode( $data ) : $data;$result = self::$redis->hset( $key_name, $key, $data );}else{$result = false;}return $result;}/*** 根据key_name和key获取所有键值对或者指定键值对* @author liwuming* @date 2019-04-27*/public function get_redis_hash($key_name = '', $item = '', $is_array = false, $field = ''){$result = false;if(!empty( $key_name )){// 首先根据key-name判断散列的键值对数量$key_name .= config( '__LXMALL_ID__' );if(!empty( $item )){$data = self::$redis->hget( $key_name, $item );if(!empty( $data )){if(!empty( $is_array )){$data = json_decode( $data, true );if(!empty( $field ) && array_key_exists( $field, $data )){$result = $data[$field];}else{$result = $data;}}else{$result = $data;}}}else{$key_list = self::$redis->hkeys( $key_name );if(!empty( $key_list )){$result = [];if(empty($is_array)){foreach( $key_list as $key_info ){$shop_info = self::$redis->hget( $key_name, $key_info );$result[$key_info] = $shop_info;}}else{foreach( $key_list as $key_info ){$shop_info = self::$redis->hget( $key_name, $key_info );$data = json_decode( $shop_info, true );$result[$key_info] = $data;}}}}}return $result;}/*** 根据key_name获取hash包含的所有键* @author liwuming* @date 2019-04-27*/public function redis_hash_keys($key_name = ''){$result = false;if(!empty( $key_name )){$key_name .= config( '__LXMALL_ID__' );$result = self::$redis->hkeys( $key_name );}return $result;}/*** 根据key_name获取hash包含的所有值* @author liwuming* @date 2019-04-27*/public function redis_hash_values($key_name = ''){$result = false;if(!empty( $key_name )){$key_name .= config( '__LXMALL_ID__' );$result = self::$redis->hvals( $key_name );}return $result;}/*** 根据key_name获取hash包含的键值对的数量* @author liwuming* @date 2019-04-27*/public function redis_hash_length($key_name = ''){$result = false;if(!empty( $key_name )){$key_name .= config( '__LXMALL_ID__' );$result = self::$redis->hlen( $key_name );}return $result;}/*** 更新redis hash表下单个键名对应json值* Created by PhpStorm.* @Author zhangzhaoyaun* Date: 2019/4/4 14:03*/public function updateRidesHash($redisname, $hashkey, $field = '*', $post){$json = get_redis_hash( $redisname, $hashkey );if($json){$data = json_decode( $json, true );if($field == '*'){foreach( $post as $key => $value ){$data[$key] = $post[$key];}}else{$fieldarr = explode( ',', $field );foreach( $fieldarr as $key => $value ){isset( $post[$value] ) && $data[$value] = $post[$value];}}$json = json_encode( $data, JSON_UNESCAPED_UNICODE );}else{$json = json_encode( $post, JSON_UNESCAPED_UNICODE );}put_redis_hash( $redisname, $hashkey, $json );}/*** 根据key_name和key删除指定的hash缓存* @author liwuming* @date 2019-04-27*/public function del_redis_hash($key_name = '', $item = ''){$result = 0;if(!empty( $key_name )){$key_name .= config( '__LXMALL_ID__' );if(! is_array( $item )){$item = [ $item ];}foreach( $item as $tmp_item ){$tmp = self::$redis->hdel( $key_name, $tmp_item );if($tmp){$result ++;}}}return $result;}/*********************hash操作 end************************//*********************list操作 start***********************//*** 根据where条件将查询结果以list形式写入缓存* @author liwuming* @params [];* @date 2019-04-27*/public function mysql_to_list($key_name = '', $table = '', $fields = '*', $where = [], $order = ['id' => 'desc'], $field = 'id', $unique = true, $type = 'l'){$result = 0;try{if(!empty( $key_name ) && !empty( $table )){$tables = is_object( $table ) ? $table : Db::name( $table );$data_list = $tables->field( $fields )->where( $where )->order( $order )->select()->toArray();if(!empty( $data_list )){$tmp_key_name = $key_name;$key_name .= config( '__LXMALL_ID__' );// 定义操作$operate = !empty( $type ) && strtolower( $type ) == 'r' ? 'LPUSH' : 'RPUSH';if(!empty( $unique )){if(!empty( $field ) && array_key_exists( $field, $data_list[0] )){$value_list = $this->get_redis_list( $tmp_key_name, 0 );foreach( $data_list as $data_info ){if(empty( $value_list ) || (!empty( $value_list ) && ! in_array( $data_info[$field], $value_list ))){$tmp = self::$redis->$operate( $key_name, $data_info[$field] );if(!empty( $tmp )){$value_list[] = $data_info[$field];$result ++;}}}}else{foreach( $data_list as $data_info ){$tmp = self::$redis->$operate( $key_name, json_encode( $data_info ) );if($tmp){$result ++;}}}}else{if(!empty( $field ) && array_key_exists( $field, $data_list[0] )){foreach( $data_list as $data_info ){$tmp = self::$redis->$operate( $key_name, $data_info[$field] );if($tmp){$result ++;}}}else{foreach( $data_list as $data_info ){$tmp = self::$redis->$operate( $key_name, json_encode( $data_info ) );if($tmp){$result ++;}}}}}}return $result;}catch( \Exception $e ){echo 121;die;p( $e->getMessage() );}}/*** 将数组中的数据信息以list形式写入缓存* @author liwuming* @date 2019-04-27*/public function array_to_list($key_name = '', $data_list = [], $type = 'l', $field = ''){$result = 0;if(!empty( $key_name ) && !empty( $data_list ) && is_array( $data_list )){$key_name .= config( '__LXMALL_ID__' );// 定义操作$operate = $type == 'l' ? 'lPush' : 'Rpush';if(!empty( $field )){foreach( $data_list as $data_info ){if(is_array( $data_info ) && array_key_exists( $field, $data_info )){$tmp = self::$redis->$operate( $key_name, $data_info[$field] );if($tmp){$result ++;}}}}else{foreach( $data_list as $data_info ){$data_info = is_array( $data_info ) ? json_encode( $data_info ) : $data_info;$tmp = self::$redis->$operate( $key_name, $data_info );if($tmp){$result ++;}}}}return $result;}/*** 判断某个值在list列表中是否存在* @author liwuming* @date 2019-04-27*/public function redis_list_exists($key_name, $value = ''){$result = false;if(!empty( $key_name ) && isset( $value )){$key_name .= config( '__LXMALL_ID__' );$value_list = $this->get_redis_list( $key_name, 0 );if(empty( $value_list )){$result = 1;}else if(!empty( $value_list )){$result = in_array( $value, $value_list ) ? 0 : 1;}}return $result;}/*** 设置单个list值* @author liwuming* @date 2019-04-27*/public function set_redis_list($key_name, $value, $unique = true, $type = 'l'){$result = false;if(!empty( $key_name ) && isset( $value )){// 定义操作$operate = !empty( $type ) && strtolower( $type ) == 'r' ? 'Rpush' : 'lPush';if(is_array( $value )){$key_name .= config( '__LXMALL_ID__' );$tmp = self::$redis->$operate( $key_name, json_encode( $value ) );$result = !empty( $tmp ) ? 1 : 0;}else{if(!empty( $unique ) && $this->redis_list_exists( $key_name, $value )){$key_name .= config( '__LXMALL_ID__' );$result = self::$redis->$operate( $key_name, $value );}else if(empty( $unique )){$key_name .= config( '__LXMALL_ID__' );$tmp = self::$redis->$operate( $key_name, $value );$result = !empty( $tmp ) ? 1 : 0;}}}return $result;}/*** 根据key_name获取列表从start偏移量到end偏移量范围内的所有元素* @author liwuming* @date 2019-04-27*/public function get_redis_list($key_name = '', $evp = 10, $page = 1, $is_array = false){$result = false;if(!empty( $key_name )){if(!empty( $evp ) && !empty( $page )){$length = $this->redis_list_length( $key_name );$start = ($page - 1) * $evp;$end = $start + $evp - 1;if($end > $length)$end = $length;if($start < $length && $length){$key_name .= config( '__LXMALL_ID__' );if(empty( $params['is_array'] )){$result = self::$redis->lrange( $key_name, $start, $end );}else{$tmp_data_list = self::$redis->lrange( $key_name, $start, $end );foreach( $tmp_data_list as $tmp_data ){$result[] = json_decode( $tmp_data, true );}}}else if(empty( $length )){$result = [];}else{$result = [];}}else if(empty( $evp )){$key_name .= config( '__LXMALL_ID__' );$result = self::$redis->lrange( $key_name, 0, - 1 );}}return $result;}/*** 根据key_name对列表进行修剪* @author liwuming* @date 2019-04-27*/public function redis_list_trim($key_name, $start = 0, $end = -1){$result = 0;if(!empty( $key_name )){$key_name .= config( '__LXMALL_ID__' );$result = self::$redis->ltrim( $key_name, $start, $end );}return $result;}/*** 根据key_name获取列表中元素的个数* @author liwuming* @date 2019-04-27*/public function redis_list_length($key_name){$result = 0;if(!empty( $key_name )){$key_name .= config( '__LXMALL_ID__' );$result = self::$redis->llen( $key_name );}return $result;}/*** 根据key_name从列表中删除第一个,或者最后一个元素* @author liwuming* @date 2019-04-27*/public function del_redis_list($key_name, $type = 'r'){$result = 0;if(!empty( $key_name )){$key_name .= config( '__LXMALL_ID__' );if($type == 'l'){$result = self::$redis->lpop( $key_name );}else{$result = self::$redis->rpop( $key_name );}}return $result;}/*** 根据value从列表中删除元素* @author liwuming* @date 2019-04-27*/public function remove_redis_list($key_name, $value, $count = 0){$result = 0;if(!empty( $key_name ) && isset( $value )){$key_name .= config( '__LXMALL_ID__' );$result = self::$redis->lrem( $key_name, $value, $count );}return $result;}/*********************list操作 end************************//*********************set操作 start***********************//*** 根据where条件将查询结果以set形式写入缓存* @author liwuming* @date 2019-04-27*/public function mysql_to_set($table, $fields = '*', $where = [], $order = [], $field = 'id', $key_name = ''){$result = 0;if(!empty( $table ) && !empty( $key_name )){$tables = is_object( $table ) ? $table : Db::name( $table );$data_list = $tables->field( $fields )->where( $where )->order( $order )->select();if(!empty( $data_list ) && array_key_exists( $field, $data_list[0] )){$key_name .= config( '__LXMALL_ID__' );foreach( $data_list as $data ){$tmp = self::$redis->sadd( $key_name, $data[$field] );if($tmp){$result ++;}}}}return $result;}/*** 将数组或者单个值,以set形式写入缓存* @author liwuming* @date 2019-04-27*/public function array_to_set($key_name, $item){$result = 0;if(!empty( $item )){$key_name .= config( '__LXMALL_ID__' );if(! is_array( $item )){$item = [ $item ];}foreach( $item as $tmp_item ){$tmp = self::$redis->sadd( $key_name, $tmp_item );if($tmp){$result = $result + 1;}}}return $result;}/*** 返回集合key_name包含的所有元素* @author liwuming* @date 2019-04-27*/public function get_redis_set($key_name){$key_name .= config( '__LXMALL_ID__' );$result = self::$redis->smembers( $key_name );return $result;}/*** 检查元素$item是否存在于集合$key_name中* @author liwuming* @date 2019-04-27*/public function redis_set_exists($key_name, $item){$key_name .= config( '__LXMALL_ID__' );$result = self::$redis->sismember( $key_name, $item );return $result;}/*** 从集合key_name里面删除一个或多个元素,并返回被移除元素的数量* @author liwuming* @date 2019-04-27*/public function redis_set_del($key_name, $item){$result = 0;if(!empty( $key_name )){$key_name .= config( '__LXMALL_ID__' );if(!empty( $item )){$items = is_array( $item ) ? $item : [ $item ];}else{$items = self::$redis->smembers( $key_name );}if(!empty( $items )){foreach( $items as $tmp_item ){$tmp = self::$redis->srem( $key_name, $tmp_item );if($tmp){$result ++;}}}}return $result;}//public function redis_del($key_name){if(!empty( $key_name )){$key_name .= config( '__LXMALL_ID__' );$items = self::$redis->delete( $key_name );}return $items;}/*********************set操作 end************************///redis操作字符串public function get($key_name = ''){$result = 0;if(!empty( $key_name )){$key_name .= config( '__LXMALL_ID__' );$result = self::$redis->get( $key_name );return $result;}}//设置键的过期时间public function pexpireat($key_name='',$timestamp = 0){$key_name .= config( '__LXMALL_ID__' );self::$redis->expireat($key_name,$timestamp);}/*** 删除一个redis的key都是用del方法, 不管是string,hash,list,set等类型, 都一样* @author liwuming* @date 2019-04-27*/public function del_redis_data($key_name = ''){$result = 0;if(!empty( $key_name )){$key_name .= config( '__LXMALL_ID__' );$result = self::$redis->del( $key_name );}return $result;}public function del_key($key_name = ''){$result = 0;if(!empty( $key_name )){//$all_keys = self::$redis->keys( $key_name );$key_name .= config( '__LXMALL_ID__' );$result = self::$redis->del( $key_name );}return $result;}/*** 获取redis中的所有key值* @author liwuming* @date 2019-04-27*/public function get_redis_keys(){$result = self::$redis->keys( '*' );return $result;}/*** 根据键key删除指定的数据* @author liwuming* @date 2019-04-27*/public function delete($key_name = '',$is_multi = false){$result = 0;if(!empty( $key_name )){if(empty($is_multi)){$key_name .= config( '__LXMALL_ID__' );}else{$key_name = self::$redis->keys($key_name);}$result = self::$redis->del( $key_name );}return $result;}/*** 清空redis* @author liwuming* @date 2019-04-27*/public function redis_clear(){$result = self::$redis->flushdb();return $result;}public function get_all_redis($key_name){$key_name .= config( '__LXMALL_ID__' );$result = self::$redis->lrange( $key_name, 0, - 1 );return $result;}
}
这篇关于tp5拓展redis操作模型RedisModel的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!