2018年10月18日 星期四

Design Data Structure in Redis


Redis Setup
https://redislabs.com/blog/redis-on-windows-10/
Install Ubuntu on Windows by "Windows Feature" GUI or by command line:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

> sudo apt-get update
> sudo apt-get upgrade
> sudo apt-get install redis-server
> redis-cli -v

> $ sudo vi /etc/redis/redis.conf
requirepass foobared

> sudo service redis-server restart

127.0.0.1:6379> get foo
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth foobared
OK
127.0.0.1:6379> get foo
"bar"


Data Structure

class Summary : public Item {
double orders;
double buyVolume;
double buyPrice;
double sellVolume;
double sellPrice;
double profit;
};
class Quote : public Item {

double bid;
double ask;
};

std::unordered_map<std::string, Summary> items;
std::unordered_map<std::string, Quote> items;

Redis Hashes
https://www.tutorialspoint.com/redis/redis_data_types.htm
Redis hashes are key-value pairs.
redis 127.0.0.1:6379> HMSET user:1 username tutorialspoint password 
tutorialspoint points 200 
OK 
redis 127.0.0.1:6379> HGETALL user:1  
1) "username" 
2) "tutorialspoint" 
3) "password" 
4) "tutorialspoint" 
5) "points" 
6) "200"

The above represents

class User {
std::string username;
std::string password;
int points;
};
std::unordered_map<std::string, User> users;
users["1"] = User{"name", "password", 200};


Python get set

import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0, password='foobared')
r.set('foo', 'bar')
print(r.get('foo'))


Python subscribe to Redis
import redis

def main():
    try:
        r = redis.StrictRedis(host='localhost', port=6379, db=0, password='foobared')
        p = r.pubsub()
        p.subscribe('redisChat')
        done = False
        while not done:
            message = p.get_message()
            if message:
                data = message['data']
                if isinstance(data, bytes):
                    print(data)
    except Exception as e:
        print(e)

if __name__=='__main__':
    main()



The End

沒有留言:

張貼留言

2023 Promox on Morefine N6000 16GB 512GB

2023 Promox on Morefine N6000 16GB 512GB Software Etcher 100MB (not but can be rufus-4.3.exe 1.4MB) Proxmox VE 7.4 ISO Installer (1st ISO re...