程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Installation and use of redis (Python)

編輯:Python

Recently used in the project redis To store data , In summary redis The installation and python call .

Yes redis For details, please refer to :

https://baike.baidu.com/item/Redis/6549233

https://www.cnblogs.com/powertoolsteam/p/redis.html

Redis(Remote Dictionary Server ), Remote dictionary service , Is a high-performance key-value database .

Contains a variety of data structures 、 Support network 、 Memory based 、 Optional persistent key value pair to store database , It has the following characteristics :

  • Run based on memory , High performance
  • Support distributed , Theoretically, it can be expanded infinitely
  • key-value The storage system
  • The use of open source ANSI C Language writing 、 comply with BSD agreement 、 Support network 、 Log type that can be memory based or persistent 、Key-Value database , And provide multilingual API.

Let's introduce Redis Service installation , as well as python Of api Use .

redis Installation and startup of services

I'm using linux System , Simple installation

yum install redis

Start the service

systemctl start redis

And then through ps -ef | grep 6379 With the default port number, you can see redis service

View service status

systemctl status redis

Out of Service

systemctl stop redis 

If you want to modify the file configuration, you can redis.conf Internal modification

python Use redis

1. Installation Library

Use python call redis Of API, Need to install python library

pip install redis

Use import reids Check to see if the installation was successful

2. Interface call

redis yes key-value Storage form of , It's also easy to use , Mainly set and get Two interfaces , We use the local default service to test :

 # redis The result is byte by default , We can set decode_responses=True Change to string .
redis_conn = redis.Redis(host='127.0.0.1', port= 6379 , db= 0)
redis_conn.set('key','Hello redis')
print(redis_conn.get('key'))

You can see the return key Corresponding data .

3. Use redis Storage numpy Array

If value The corresponding is numpy Array , It cannot be simply used directly set Interface , Here we need to make a transformation , stay set Data time , We will numpy transposition bytes, And then in get Data time , We will again bytes Data is restored to numpy that will do .

Main interface code :

class RedisModel:
def __init__(self,):
self.conn_redis = redis.Redis(host='127.0.0.1', port=6379, db=0)
def array_to_bytes(self, array):
""" numpy Array to bytes, Pass in redis
:return: encoded bytes
"""
h, w = array.shape
shape = struct.pack('>II', h, w)
encoded = shape + array.tobytes()
return encoded
def bytes_to_array(self, bytes):
""" redis From values Convert to numpy
:return: array
"""
h, w = struct.unpack('>II', bytes[:8])
# Note that there dtype Consistent with input , Prevent data length alignment problems
a = np.frombuffer(bytes[8:], dtype=np.float32).reshape(h, w)
return a
def get(self, key):
""" obtain redis Inside key Corresponding packet
:param key: room_id
:return: value(M*N matrix ),key non-existent , return None
"""
# value = None
# if self.conn_redis.exists(key):
value = self.conn_redis.get(key)
value = self.bytes_to_array(value)
return value
def set(self, key, item):
""" New information writing redis
:param key: ****
:param item:*****
"""
b_value = self.array_to_bytes(item)
self.conn_redis.set(key, b_value)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved