zoukankan      html  css  js  c++  java
  • python浅学【网络服务中间件】之Redis

    一、关于NoSQL:

    NoSQL(NoSQL = Not Only SQL ),"不仅仅是SQL"。

    相比MySQL等关系型数据库,NoSQL为非关系型的数据存储

    Nosql中比较火的三个数据库有:Redis、Memchache、MongoDb。

    为什么使用NoSQL:

    为了解决大规模数据集合多重数据种类带来的挑战,尤其是大数据应用难题。

    易扩展性:NoSQL数据库种类繁多,但是一个共同的特点都是去掉关系数据库的关系型特性。数据之间无关系,这样就非常容易扩展。无形之间也在架构的层面上带来了可扩展的能力。

    高性能性:NoSQL数据库都具有非常高的读写性能,尤其在大数据量下,同样表现优秀。这得益于它的无关系性,数据库的结构简单。

    二、关于Redis:

    REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。

    Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。

    它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Hash), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。

    与memcached的对比:

    三、redis简单操作:

    以windows下为例:

    连接redis:> redis-cli.exe -h host -p port -a password   # 不输入则为默认连接

    PS D:
    edis> .
    edis-cli.exe
    127.0
    .0.1:6379> ping # 查询redis服务是否开通 PONG 127.0.0.1:6379> set test hello # 设置key的value OK 127.0.0.1:6379> get test # 查询key "hello" 127.0.0.1:6379> set test hi # 再次设置key即更改 OK 127.0.0.1:6379> get test "hi" 127.0.0.1:6379> del test # 删除key (integer) 1 127.0.0.1:6379> get test (nil)

    四、使用python实现redis发布订阅功能:

    连接redis:

    import redis
    
    sr = StrictRedis(host='localhost',port=6379,db=0)    #不输入为默认连接

    发布资讯:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:Riy
    
    
    import redis
    
    
    client = redis.Redis() 
    channels = ["NoSQL", "redis"]
    
    
    def main():
        print("可以发布到任意一个频道:")
        for i in channels:
            print(i)
    
        while True:
            ch = input("输入频道:")
            print("输入发送的信息(q:退出)")
            msg = input(">>")
            if msg == 'q':
                break
            client.publish(ch, msg)
    
    if __name__ == '__main__':
        main()

    订阅资讯:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:Riy
    
    
    import redis
    
    
    client = redis.StrictRedis(decode_responses=True)    # 设置编码,不设置则为字节流
    s = client.pubsub()
    
    name = input('选择监听频道:')
    s.subscribe(name)
    
    print('开始监听......
    ')
    
    for item in s.listen():
        if item['type'] == 'message':
            print (item)

    设置 decode_responses = True 收到的中文信息:

     不设置则收到的 data 为字节流:

  • 相关阅读:
    二分图最大匹配的König定理及其证明
    HDOJ 2389 Rain on your Parade
    HDOJ 1083 Courses
    HDOJ 2063 过山车
    POJ 1469 COURSES
    UESTC 1817 Complete Building the Houses
    POJ 3464 ACM Computer Factory
    POJ 1459 Power Network
    HDOJ 1532 Drainage Ditches
    HDU 1017 A Mathematical Curiosity
  • 原文地址:https://www.cnblogs.com/riyir/p/12550737.html
Copyright © 2011-2022 走看看