简介
REmote DIctionary Server(Redis)是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value)存储数据库,并提供多种语言的 API。Redis运行在内存中 但是可以持久化到磁盘,所以在对不同数据集进行高速读写时 需要权衡内存,因为数据量不能大于硬件内存。
Redis的数据结构类型(Redis值value的类型)有5种:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。
字符串的一个键 最大能存储 512MB。
每个 hash 可以存储 2^32 -1 键值对(40多亿)。
列表最多可存储 2^32 - 1 元素 (4294967295, 每个列表可存储40多亿)。
集合最大的成员数为 2^32 - 1(4294967295, 每个集合可存储40多亿个成员)。
http://redisdoc.com/index.html
redis-cli -h host -p port -a password
Python包
pip install redis-py-cluster==2.1.0
#!/bin/python3
# -*- coding:utf-8 -*-
import os, sys
import json
import copy
from rediscluster import RedisCluster
class RedisClient:
def __init__(self, config):
self._config = copy.deepcopy(config)
self._startup_nodes_list = self._config.nodes_list
self._password = self._config.redis_password
self.redis = RedisCluster(startup_nodes=self._startup_nodes_list,
password=self._password, decode_responses=True, socket_timeout=5)
def hsetnx(self, key, field, value, timeout=None): # set_new_field field已经存在,该操作无效
self.redis.hsetnx(key, field, value)
if isinstance(timeout, int):
self.redis.expire(key, timeout)
def hset(self, key, field=None, value=None, mapping=None, timeout=None): # field已经存在于哈希表中,旧值将被覆盖
if not field is None:
return self.redis.hset(key, field, value) # 一个key的单个filed:value添加
elif not mapping is None:
print('=====mapping=====', field, value, mapping)
return self.redis.hset(key, None, None, mapping) # 一个key的多个{fields:values}添加
if isinstance(timeout, int):
self.redis.expire(key, timeout)
def expire(self, key, timeout): # 对key执行命令的超时限制判断
if not isinstance(timeout, int):
raise ValueError("timeout must be integer")
self.redis.expire(key, timeout)
def hget(self, key, field):
value = self.redis.hget(key, field)
return "" if value is None else str(value)
def hgetall(self, key):
value = self.redis.hgetall(key)
return "" if value is None else str(value)
def hexists(self, key, field): # 判断key的指定field是否存在
return self.redis.hexists(key, field)
def hdel(self, key, field): # 删除key的指定field [单个field]
if self.redis.hexists(key, field):
return self.redis.hdel(key, field)
else: pass
#def hdel(self, key, fields): # 删除key的指定field key(string), fields=(filed1name,field2name,field3name, ...)
# return self.redis.hdel(key, *fields)
def close(self):
self.redis.close()
class config:
def __init__(self, _nodes_list, redis_password):
self.nodes_list = _nodes_list
self.redis_password = redis_password
if __name__=="__main__":
conf = config([{'host':'redis.db.cloud', 'port':'10009'}],
'123456')
client = RedisClient(conf)
user_table = 'user_feature1_'
item_table = 'item_feature1_'
uid_key = user_table + '7db9ca895802365'
uid_field = 'user_behavior_timezone'
print('the uid_key is : %s, uid_field is : %s' % (uid_key, uid_field))
print('test_hgetall_ing......')
print(client.hgetall(uid_key))
print('\n')
print('test_hexists_ing......')
print(client.hexists(uid_key, uid_field))
print('test_hget_ing......')
print(client.hget(uid_key, uid_field))
print('\n')
print('test_hset_ing......')
print(client.hset(uid_key, uid_field, 'N8'), 'after_change_field: ', client.hget(uid_key, uid_field))
print(client.hset(uid_key, None, None, mapping={uid_field: '8'}), 'recover_field: ', client.hget(uid_key, uid_field))
print('\n')
print('test_hdel_ing......')
print(client.hset(uid_key, uid_field, '8'), '%s is exists: ' % uid_field, client.hexists(uid_key, uid_field), client.hdel(uid_key, uid_field))
print('after hdel %s is exist: '% uid_field, client.hexists(uid_key, uid_field), client.hset(uid_key, uid_field, '8'))
client.close()