#!/usr/bin/env python
#-*-coding:UTF-8-*-
"""
@Item : redis API
@Author : william
@Group : DEV Group
@Date : 2014-04-04
@Funtion:
redis_set : Redis in the form of pipe insert data, json hash as a string print
redis_get : Redis inprintion, and json string into the original print
"""
import sys,os,time,redis,traceback,json
from abc import log
from abc import config
LOG = log.get_logger(__name__)
class redising(object):
''' Establish redis session connection pool '''
def __init__ (self,host,port,db):
self.host = host
self.port = port
self.db = db
try:
pool = redis.ConnectionPool(host = self.host, port = self.port, db = self.db)
self.conn = redis.Redis(connection_pool=pool)
self.pipe = self.conn.pipeline()
except:
LOG.error(traceback.format_exc())
return traceback.format_exc()
def redis_set(self,keys=None ,values=None):
''' Insert redis databases,keys = key ,values = value'''
try:
self.pipe.set(keys,json.dumps(values))
self.pipe.execute()
except:
LOG.error(traceback.format_exc())
return traceback.format_exc()
def redis_hset(self, key, field, values):
'''insert redis key, field ,values'''
try:
self.pipe.hset(key, field, json.dumps(values))
self.pipe.execute()
except:
LOG.error(traceback.format_exc())
return traceback.format_exc()
def redis_hget(self, key, field = None):
'''get by key, return the dict'''
try:
fields = []
if field:
fields.append(field)
else:
fields = self.conn.hkeys(key)
for f in fields:
self.pipe.hget(key, f)
values = self.pipe.execute()
values = [json.loads(i) for i in values]
return dict(zip(fields, values))
except:
return key
def redis_get(self,argv):
''' Getting single KYES values ,argv : is keys'''
try:
self.pipe.get(argv)
return json.loads( self.pipe.execute()[0])
except:
LOG.error(traceback.format_exc())
return traceback.format_exc()
def redis_getkeys(self):
''' Getting all keys '''
try:
self.pipe.keys('*')
return self.pipe.execute()[0]
except:
LOG.error(traceback.format_exc())
return traceback.format_exc()
def redis_delete(self,keys):
try:
self.pipe.delete(keys)
self.pipe.execute()
return 'Delete True'
except:
LOG.error(traceback.format_exc())
return 'Delete False'