forked from shidenggui/easyquant
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request shidenggui#48 from ruyiqf/develop
Develop
- Loading branch information
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .easyredis import RedisIo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# coding: utf-8 | ||
import os | ||
import sys | ||
import redis | ||
import json | ||
|
||
from ..log_handler import DefaultLogHandler | ||
|
||
"""Debug""" | ||
""" | ||
sys.path.append('..') | ||
from log_handler import DefaultLogHandler | ||
""" | ||
|
||
class RedisIo(object): | ||
"""Redis操作类""" | ||
|
||
def __init__(self, conf): | ||
self.config = self.file2dict(conf) | ||
if self.config['passwd'] is None: | ||
self.r = redis.Redis(host=self.config['redisip'], port=self.config['redisport'], db=self.config['db']) | ||
else: | ||
self.r = redis.Redis(host=self.config['redisip'], port=self.config['redisport'], db=self.config['db'], password = self.config['passwd']) | ||
self.log = self.log_handler() | ||
|
||
def file2dict(self, path): | ||
#读取配置文件 | ||
with open(path) as f: | ||
return json.load(f) | ||
|
||
def cleanup(self): | ||
#清理Redis当前数据库 | ||
self.r.flushdb() | ||
|
||
def lookup_redist_info(self): | ||
#查询Redis配置 | ||
info = self.r.info() | ||
for key in info: | ||
self.log.info('%s:%s' % (key, info[key])) | ||
|
||
def set_key_value(self, key, value): | ||
#设置键值对key<-->value | ||
self.r.set(key, value) | ||
|
||
def get_key_value(self, key): | ||
#查询键值对 | ||
return self.r.get(key) | ||
|
||
def save(self): | ||
#强行保存数据到硬盘 | ||
return self.r.save() | ||
|
||
def get_keys(self): | ||
#获取当前数据库里面所有键值 | ||
return self.r.keys() | ||
|
||
def delete_key(self, key): | ||
#删除某个键 | ||
return self.r.delete(key) | ||
|
||
def push_list_value(self, listname, value): | ||
#推入到队列 | ||
return self.r.lpush(listname, value) | ||
|
||
def pull_list_range(self, listname, starpos=0, endpos=-1): | ||
#获取队列某个连续片段 | ||
return self.r.lrange(listname, starpos, endpos) | ||
|
||
def get_list_len(self, listname): | ||
#获取队列长度 | ||
return self.r.llen(listname) | ||
|
||
def log_handler(self): | ||
#重定向日志 | ||
return DefaultLogHandler() | ||
|
||
def main(): | ||
ri = RedisIo('redis.conf') | ||
ri.lookup_redist_info() | ||
ri.set_key_value('test1', 1) | ||
ri.log.info(ri.get_key_value('test1')) | ||
ri.push_list_value('test2', 1) | ||
ri.push_list_value('test2', 2) | ||
ri.log.info(ri.pull_list_range('test2',0,-1)) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"redisip": "", | ||
"redisport": "", | ||
"passwd": "", | ||
"db": 0 | ||
} | ||
|