python封装redis的api

开发环境:

 

      mac

      python

      redis

      pypi/redis    https://pypi.python.org/pypi/redis

 

步骤:

      1.

      安装运行主体redis 

      http://redis.io/

      目前最新版本是3.0.7

      

       2.

      安装pyi/redis

     验证redis的涵数功能

     
python封装redis的api
 

 

        3.

       安装django的restful_api 模块

        http://www.django-rest-framework.org/#installation

        

Requirements

REST framework requires the following:

  • Python (2.7, 3.2, 3.3, 3.4, 3.5)
  • Django (1.7+, 1.8, 1.9)

The following packages are optional:

Installation

Install using pip, including any optional packages you want...

pip install djangorestframework
pip install markdown       # Markdown support for the browsable API.
pip install django-filter  # Filtering support

...or clone the project from github.

git clone [email protected].com:tomchristie/django-rest-framework.git

Add 'rest_framework' to your INSTALLED_APPS setting.

INSTALLED_APPS =(...'rest_framework',)

 

         代码部分:

        

          import redis

class pyRedis:
    def __init__(self):
        self.host = '127.0.0.1'
self.port = '6379'
self.db = 0
# self.c = redis.StrictRedis(host=self.host,port=self.port,db=self.db)
pool = redis.ConnectionPool(host=self.host,port=self.port,db=self.db)
        self.r = redis.Redis(connection_pool=pool)
        # self.c = redis.Redis(host=self.host,port=self.port,db=self.db)
def set_key(self,key,valuse):
        try:
            return  self.r.set(key,valuse)
        except Exception as e:
            print  e.message

    def get_key(self,key):
        try:
            return self.r.get(key)
        except Exception as e:
            print  e.message