Redis install, 데이터 처리

linux redis 설치

apt-get install redis

linux redis 실행

시작
systemctl start redis
종료
systemctl enable redis
설치 확인
redis-cli ping
*PONG이 나오면 설치 성공
redis 접속
redis-cli

redis server 설치

Downloads · dmajkic/redis (github.com)

python redis설치

pip3 install redis

python redis 사용

단일 데이터 처리 get / set

import redis
rd = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
rd.set('key1','helloword')
print(rd.get(key1))

리스트 데이터 처리 lpush / lrange

for item in con.lrange(key,0,-1):
    print(item.decode())

redis json 함수 사용

import redis
import json
sensorList = ['Temperature','humiity','quantum','par','spectral','ec','qh','moisture']
con = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
#json 형태로 key 값에 저장
def addObject(key,objects):
    jsonV = json.dumps(objects)
    print(con.lpush(key,jsonV))
#key 값 해당하는 데이터 제거
def delObject(key):
     con.delete(key)
#key 값 전체 load
def getAll(key):
    for item in con.lrange(key,0,-1):
        print(item.decode())
if __name__ == '__main__':
    #센서 데이터 담을 딕셔너리 초기화
    tempObject = {}
    #상단에서 정의한 센서 리스트를 tempObject에 값과 함게 담는다
    for i in sensorList:
       tempObject[i] = i + " testvalues"
    #센서 값을 담은 키와 딕셔너리를 addObject 함수로 보내 redis에 저장한다.
    addObject('1',tempObject)
    #키값을 getAll 함수에 보내 redis에서 읽어와 출력한다
    getAll('1')

Leave a Comment