redis安装/redis普通链接和连接池/redis字符串类型/redis hash类型/redis列表操作/redis其它操作

Redis介绍和安装

# Redis :软件,存储数据的,速度非常快,redis是一个key-value存储系统(没有表的概念),cs架构的软件
    -服务端  客户端(python作为客户端,java,go,图形化界面,命令窗口的命令)
# es:存数据的地方

# 关系型数据库和非关系型数据库
    -关系型:mysql,PostgreSQL,oracle,sqlserver,db2
        -PG
        -去 IOE:国产化
            -IBM---》浪潮信息,曙光,联想
            -Oracle---》数据----》达梦。。。。
            -EMC存储--》国产存储
    -非关系型数据库(nosql):redis(缓存),mongodb(json文档数据存储),es(大数据量存储)。。。。
        -nosql 指非关系型数据库: no only sql,对关系型数据库的补充
        
# redis特点:
    -开源软件,存数据,cs架构
    -key-value存储 ,5大数据类型  value的类型是5种:字符串,hash(字典),列表,集合,有序集合
    -速度快:
        -1 纯内存存储(核心)
        -2 使用了IO多路复用的网络模型
        -3 数据操作是单线程,避免了线程间切换,而且没有锁,也不会数据错乱
    -支持持久化
        -纯内存,可以存到硬盘上,防止数据丢失
    -redis又被称之为 缓存数据库

安装redis

# redis 是用c语言编写的,需要在不同平台编译成可执行文件,才能在这个平台上运行
    -redis 使用了io多路复用种的epoll模型,win不支持epoll
    -redis官方,不支持win版本
    -微软官方,就把redis改动,编译成可执行,能运行在win上,滞后 3.x版本
    -第三方:5.x版本
    
# redis 官方网:https://redis.io/download/
# redis中文网:http://redis.cn
# win:3.x:https://github.com/microsoftarchive/redis/releases
# win:5.x:https://github.com/tporadowski/redis/releases/


# 安装:一路下一步
    -安装完成后,在安装路径下有
        -redis-cli.exe     # mysql
        -redis-server.exe   # mysqld
        -redis.windows-service.conf  # my.ini
     -并且会自动做成服务
        -服务的命令:redis-server.exe  redis.windows-service.conf
        
        
        
        
# 启动redis服务端
    -1 命令行中  redis 就可以启动服务
    -2 命令行中,启动服务,并指定配置文件
        redis-server 配置文件路径
    -3 使用服务启动

# 客户端链接
    -1 命令行客户端:
        -redis-cli  # 默认连本地的6379端口
        -redis-cli -p 6379 -h 127.0.0.1
    -2 图形化客户端链接
        -1 最新版的Navicate支持链接redis了(收费的)
        -2 Redis Desktop Manager(https://resp.app/)  收费的  用的多  qt写图形化界面
            -qt是个平台,做GUI[图形化界面]开发
            -用c写,用python写  pyqt5
            
    -3 python的模块
        -pip3 install redis -i https://pypi.douban.com/simple
        

redis普通链接和连接池

# 一定要保证,池是单例的,以模块导入的形式做成了单例

# pool.py
import redis
POOL = redis.ConnectionPool(max_connections=1000,host='127.0.0.1',port=6379)

# 其它.py

import redis
from pool import POOL # 模块导入的方式, 天然单例
conn = redis.Redis(connection_pool=POOL)  # 以后拿到链接,是从POOL种取,如果没有可用的了,默认不阻塞,可以通过某个参数配置,设置阻塞等待
res = conn.get('name')  # 获取key名为name的value值
print(res)
conn.close()  # 关闭链接

普通链接

from redis import Redis

# conn = Redis()  # 建立redis的链接
conn = Redis(host="127.0.0.1",
             port=6379,
             db=0,decode_responses=True)  # 建立redis的链接   decode_responses=True,查询回来返回的结果是字符串类型,否则是byte格式
res = conn.get('name')  # 获取key名为name的value值
print(res)
conn.close()  # 关闭链接

连接池链接

# 一定要保证,池是单例的,以模块导入的形式做成了单例

# pool.py
import redis
POOL = redis.ConnectionPool(max_connections=1000,host='127.0.0.1',port=6379)

# 其它.py

import redis
from pool import POOL # 模块导入的方式, 天然单例
conn = redis.Redis(connection_pool=POOL)  # 以后拿到链接,是从POOL种取,如果没有可用的了,默认不阻塞,可以通过某个参数配置,设置阻塞等待
res = conn.get('name')  # 获取key名为name的value值
print(res)
conn.close()  # 关闭链接

redis字符串类型

# https://www.cnblogs.com/liuqingzheng/articles/9833534.html

'''
1 set(name, value, ex=None, px=None, nx=False, xx=False)
2 setnx(name, value)
3 psetex(name, time_ms, value)
4 mset(*args, **kwargs)
5 get(name)
6 mget(keys, *args)
7 getset(name, value)
8 getrange(key, start, end)
9 setrange(name, offset, value)
10 setbit(name, offset, value)
11 getbit(name, offset)
12 bitcount(key, start=None, end=None)
13 bitop(operation, dest, *keys)
14 strlen(name)
15 incr(self, name, amount=1)
16 incrbyfloat(self, name, amount=1.0)
17 decr(self, name, amount=1)
18 append(key, value)
'''

import redis

conn = redis.Redis()
# 1 set(name, value, ex=None, px=None, nx=False, xx=False)
# ex,过期时间(秒)
# px,过期时间(毫秒)
# nx,如果设置为True,则只有name不存在时,当前set操作才执行, 值存在,就修改不了,执行没效果
# xx,如果设置为True,则只有name存在时,当前set操作才执行,值存在才能修改,值不存在,不会设置新值
# conn.set('hobby', '篮球', ex=6)
# conn.set('hobby', '篮球', px=3000)
# conn.set('hobby', '足球', nx=True)
# conn.set('hobby', '足球', xx=True)

# 2 setnx(name, value)
# conn.setnx('hobby1', '乒乓球')  # 等同于conn.set('hobby', '足球', nx=True)

# 3 psetex(name, time_ms, value)
# conn.psetex('name',3000,'xxx')

# 4 mset(*args, **kwargs)
# conn.mset({'name': "lqz", 'height': 183})  # 跟一次次设置的区别是,少了网络交互的时间

# 5 get(name)
# res=conn.get('hobby')  # utf-8 编码,一个中文占3个字节    GBK 编码,一个中文占2个字节
# print(res)


# 6 mget(keys, *args)
# res=conn.mget('name','age')
# res=conn.mget(['name','age'])
# print(res)


# 7 getset(name, value)
# res=conn.getset('name','彭于晏')
# print(res)

# 8 getrange(key, start, end)
# res=conn.getrange('name',0,1)  # 前闭后闭区间,拿的是字节,不是字符
# print(res)

# 9 setrange(name, offset, value)
# conn.setrange('name',2,'lqz')

# 14 strlen(name)
# res=conn.strlen('name')  # 9 统计字节长度
# print(res)


# 15 incr(self, name, amount=1)  # 做计数器。不会出现并非安全问题
# conn.incrby('height')

# 16 incrbyfloat(self, name, amount=1.0)
# 17 decrby(self, name, amount=1)
# conn.decrby('height',5)


# 18 append(key, value)
conn.append('name', 'xxxx')

conn.close()


'''
get
set
strlen
append
'''

redis hash类型

'''


1 hset(name, key, value)

2 hmset(name, mapping)
3 hget(name,key)

4 hmget(name, keys, *args)

5 hgetall(name)

6 hlen(name)

7 hkeys(name)
8 hvals(name)

9 hexists(name, key)

10 hdel(name,*keys)

11 hincrby(name, key, amount=1)
12 hincrbyfloat(name, key, amount=1.0)
13 他们是一家
hscan(name, cursor=0, match=None, count=None)
hscan_iter(name, match=None, count=None)

'''
import redis

conn = redis.Redis(decode_responses=True)
# 1 hset(name, key, value)
# conn.hset('userinfo', 'name', '刘亦菲')
# conn.hset('userinfo', 'age', '38')
# 2 hmset(name, mapping) # 弃用了,还能用
# conn.hmset('userinfo2', {'name': "彭于晏", 'height': 183})
# conn.hset('userinfo3', mapping={'name': "彭于晏123", 'height': 183})

# 3 hget(name,key)
# res=conn.hget('userinfo','age')
# print(res)

# 4 hmget(name, keys, *args)
# res=conn.hmget('userinfo',['name','age'])
# print(res)

# 5 hgetall(name)  # 慎用,如果hash中key非常的,可能会撑爆内存
# res=conn.hgetall('userinfo')
# print(res)

# 6 hlen(name)
# res=conn.hlen('userinfo')
# print(res)
# 7 hkeys(name)
# print(conn.hkeys('userinfo'))
# 8 hvals(name)
# print(conn.hvals('userinfo'))
# 9 hexists(name, key)
# print(conn.hexists('userinfo','name'))

# 10 hdel(name,*keys)
# conn.hdel('userinfo2', 'name', 'height')


# 11 hincrby(name, key, amount=1)
# conn.hincrby('userinfo3','height')
# 12 hincrbyfloat(name, key, amount=1.0)

# 13 他们是一家
# for i in range(1000):
#     conn.hset('hash_test', 'egg_%s' % i, '鸡蛋%s号' % i)


# res=conn.hgetall('hash_test')
# print(res)
# hscan(name, cursor=0, match=None, count=None)  分批获取,获取多少个,约等于,它不单独用
# res = conn.hscan('hash_test', cursor=0, count=20)
# print(len(res[1]))

# hscan_iter(name, match=None, count=None)  # 获取所有,但是分批获取
# res=conn.hscan_iter('hash_test',count=10)  # 取出所有,每次拿10条,用完再取10条,直到取完,内部使用了hscan+生成器
# for item in res:
#     print(item)


conn.close()


'''
hset
hget
hlen
hexisit

'''

redis列表操作

'''
lpush(name,values)
rpush(name, values) 表示从右向左操作
lpushx(name,value)
rpushx(name, value) 表示从右向左操作
llen(name)
linsert(name, where, refvalue, value))
lset(name, index, value)
lrem(name, value, num)
lpop(name)
rpop(name) 表示从右向左操作
lindex(name, index)
lrange(name, start, end)
ltrim(name, start, end)
rpoplpush(src, dst)
blpop(keys, timeout)
brpoplpush(src, dst, timeout=0)
自定义增量迭代
'''

import redis

conn = redis.Redis(decode_responses=True)


# lpush(name,values)    左侧插入值  [ 乒乓球  足球 篮球]
# conn.lpush('hobbys', '篮球')
# conn.lpush('hobbys', '足球')
# conn.lpush('hobbys', '乒乓球')


# rpush(name, values) 表示从右向左操作
# conn.rpush('hobbys', '橄榄球')

# lpushx(name,value)  # 只有key存在才能插入,从左侧插入
# conn.lpushx('hobbys1', '排球')
# rpushx(name, value) 只有key存在才能插入表示从右向左操作

# llen(name)
# print(conn.llen('hobbys'))

# name:rediskey值,
# where: before   after,
# refvalue: 在谁的前后
# value:插入的数据
# linsert(name, where, refvalue, value))
# conn.linsert('hobbys', 'after', '乒乓球', '保龄球')
# conn.linsert('hobbys', 'before', '乒乓球', '球球')


# lset(name, index, value)
# conn.lset('hobbys', 1, '球球球')

# lrem(name,num, value )
# conn.lrem('hobbys',0,'排球')  # 0 表示全删除
# conn.lrem('hobbys', 1, '足球')  # 正数表示从左侧删除
# conn.lrem('hobbys', -2, '足球')  # 负数表示从右侧删除

# lpop(name)
# print(conn.lpop('hobbys'))  # 从左侧弹出
# print(conn.rpop('hobbys'))
# rpop(name) 表示从右向左操作

# lindex(name, index)

# res=conn.lindex('hobbys',2)
# print(res.decode('utf-8'))

# lrange(name, start, end)
# res=conn.lrange('hobbys',0,2)  # 前闭后闭区间
# print(res)


# ltrim(name, start, end)

# res=conn.ltrim('hobbys',1,3)
# print(res)

# rpoplpush(src, dst)

# blpop(keys, timeout) 着重讲,这个就是简单的消息队列,可以实现分布式的程序----》生产者消费者模型

# res=conn.blpop('hobby',timeout=2)  # 只要列表中没有值,就会阻塞,有值才会继续运行
# print(res)

# brpoplpush(src, dst, timeout=0)

# 自定义增量迭代
# 一次性把列表中所有数据取出来
# for i in range(1000):
#     conn.lpush('eggs','鸡蛋%s号'%i)
# l=conn.llen('eggs')
# res=conn.lrange('eggs',0,l)
# print(res)

def l_scan_iter(key, count=10):
    num = 0
    while True:
        print('-----------')
        res = conn.lrange(key, num, num + count - 1)
        num = num + count
        if res:
            for item in res:
                yield item
        else:
            break


for item in l_scan_iter('eggs', 20):
    print(item)

conn.close()



'''
lpush
lpop
linsert
lset
llen
lrange
'''

redis其它操作

'''

delete(*names)
exists(name)
keys(pattern='*')
expire(name ,time)
rename(src, dst)
move(name, db))
randomkey()
type(name)

'''

import redis

conn=redis.Redis()
# delete(*names)
# conn.delete('eggs','hobbys')
# exists(name)
# conn.set('name','lqz')

# print(conn.exists('name'))  #

# keys(pattern='*')
# res=conn.keys('*')
# res=conn.keys('nam?')
# print(res)

# expire(name ,time)
# conn.expire('hobby',5)

# rename(src, dst)
# conn.rename('name','name1')

# move(name, db))
# conn.move('name1',2)

# randomkey()
# res=conn.randomkey()
# print(res)

# type(name)
res=conn.type('name1')
print(res)
conn.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑猪去兜风z1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值