记录一个python通用的调用类

# -*- coding: utf-8 -*-

import redis
import json
from config import OFFICIAL_APIS


class Redis_read_write:

@staticmethod
def get_redis():
"""
获取 Redis 的连接
"""
r = redis.Redis(host=OFFICIAL_APIS['redis_ip'], port=OFFICIAL_APIS['redis_port'], username=None, password=None,
db=0)
return r

# set_data(key_name, access_token, ex=7100)
@staticmethod
def set_data(name, value, **kwargs):
# 将数据存入redis缓存
r = Redis_read_write.get_redis()
value = json.dumps(value)
r.set(name, value, **kwargs)

# get_data(key_name)
@staticmethod
def get_data(name):
# 取出key为name的值
r = Redis_read_write.get_redis()
value = r.get(name)
if value:
value = json.loads(value)
return