Skip to content

Commit

Permalink
Resdie客户端
Browse files Browse the repository at this point in the history
  • Loading branch information
bernieyangmh committed Dec 20, 2016
1 parent 28787fb commit 35b6714
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions DB/RedisClient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
# !/usr/bin/env python

'''
self.name为Redis中的一个key
'''

import random
import json
import redis

class ReidsClient(object):
"""
Reids client
"""

def __init__(self, name, host, port):
"""
init
:param name:
:param host:
:param port:
:return:
"""
self.name = name
self.__conn = redis.Redis(host=host, port=port, db=0)

def get(self):
"""
get an item
:return:
"""
values = self.__conn.smembers(name=self.name)

return random.choice(list(values)) if values else None

def put(self, value):
"""
put an item
:param value:
:return:
"""
value = json.dump(value, ensure_ascii=False).encode('utf-8') if isinstance(value, (dict, list)) else value
return self.__conn.sadd(self.name, value)

def pop(self):
"""
pop an item
:return:
"""
value = self.get()
if value:
self.__conn.spop(self.name, value)
return value

def delete(self, value):
"""
delete an item
:param key:
:return:
"""
self.__conn.srem(self.name, value)

def getAll(self):
return self.__conn.smembers(self.name)


0 comments on commit 35b6714

Please sign in to comment.