Python操作Redis:键(Key)
Python的redis模块管理键(Key)方法主要实现的Redis命令包括KEYS、GET、DEL(delete)、EXISTS、RANDOMKEY,TYPE、EXPIRE,其他的一些不能实现的在代码注释里面有所体现。

实验环境

Linux系统版本

LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: RedHatEnterpriseServer
Description:    Red Hat Enterprise Linux Server release 6.4 (Santiago)
Release:    6.4
Codename:   Santiago

Redis版本

127.0.0.1:6379> info
# Server
redis_version:3.0.7

Python版本

Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Python的Redis模块版本

DATA
    __all__ = ['Redis', 'ConnectionPool', 'RedisError', 'ConnectionError',...
    __version__ = '2.0.0'

VERSION
    2.0.0
函数说明
1. set/get : 设置或者获取指定key的值,set/get key value
2. delete : 函数实现redis数据库DEL命令功能,delete key/DEL key
3. exists : 检测key存在与否,返回1或者0, exists key_name
4. expire : 设置过期时间,以秒计,expire key time
5. keys : 查找所有符合给定模式( pattern)的键 , keys pattern
6. type : 返回键(Key)对应的值, type key
7. randomKey : 返回当前数据库随机键对应的值,randomkey()/RANDOMKEY
8. 由于Python的redis模块原因,Redis客户端有关键(KEY)的命令目前暂无可实现方法的代码事例。

代码示例

#!/usr/bin/python
import redis
import time
## Connect local redis service
client =redis.Redis(host='127.0.0.1',port=6379,db=0)
print "Connection to server successfully!"
dicKeys = client.keys("*")
print dicKeys

### Redis Key command part Start ###

# Set key-vlaue and get key-value
client.set('w3ckey','redis')
val = client.get('w3ckey')
print "Get key-vlaue ", val

# Delete key w3ckey
client.delete('w3ckey')
val = client.get('w3ckey')
print "Get none key-vlaue: ", val

# No dump key-value
client.set('greeting','Hello, dumping world!')
val = client.get('greeting')
print "Get key-vlaue ", val

# Exists check
keyList = ['w3ckey','greeting']
for key in keyList:
#    print "Key name: ",key
    isKey =client.exists(key)
    if isKey :
        print "Have value mapping with key: ", key
    else:
        print "No value mapping with key: ",key
# Set Expire time for greeting
client.expire('greeting',2)
#saveTime = client.pttl('greeting')
#print "Remaining time: ",saveTime
time.sleep(2)
isExpire = client.get('greeting')
if not isExpire:
    print "Key expire "
else:
    print "Key not expire"

# Key get special pattern, Redis command:keys
#set value
keyDic = {'w3c1':'redis', 'w3c2':'mysql', 'w3c3':'mongodb'}
for key in keyDic.keys():
    client.set(key,keyDic[key])
keyList = client.keys("w3c*")
print "Get keys: ",keyList

# Get random key from DB, Redis command:randomkey
randomKey =client.randomkey()
print "Get random key: ",randomKey

# Get key type, Redis command:type
keyType =client.type(randomKey)
print "Get key type: ",keyType

# Push value to list's head
client.lpush('w3ckey','redis')
client.lpush('w3ckey','mongodb')
client.lpush('w3ckey','mysql')
val = client.lrange('w3ckey',0,3)
print "Get key-vlaue list: ", val

#Empty db
client.flushdb()

hashVal = client.hgetall('profile')
print hashVal
参考资料
1、Redis 键(key)
2、Python redis文档(python交互模式下命令>>>help redis)



Python操作Redis:键(Key)相关推荐

  1. python 操作redis之——有序集合(sorted set) (七)

    #coding:utf8 import redis r =redis.Redis(host="23.226.74.190",port=63279,password="66 ...

  2. python 操作redis之——HyperLogLog (八)

    #coding:utf8 import redis # python 操作redis之--HyperLogLog r =redis.Redis(host="33.23.724.12190&q ...

  3. python操作redis set_Python操作redis学习系列之(集合)set,redis set详解 (六)

    #-*- coding: utf-8 -*- importredis r= redis.Redis(host="126.56.74.190",port=639,password=& ...

  4. Python操作Redis及连接方式

    前沿:随着互联网的高速发展,数据变得越来越重要,Python成为了人工智能的热门语言,而Nosql数据库已成为日常开发用品. 今天要写的是Python操作Redis Redis的安装我就不介绍了,你可 ...

  5. python操作redis用法详解

    1.简单介绍 安装redis pip install redis Redis是一个基于内存的高效的键值型非关系型数据库,存取效率极高,而且支持多种存储数据结构,使用也非常简单.本节中,我们就来介绍一下 ...

  6. Python操作Redis中的hash

    Redis 数据库hash数据类型是一个string类型的key和value的映射表,适用于存储对象.Redis 中每个 hash 可以存储 232 - 1 键值对(40多亿).  Python的re ...

  7. Python 操作 redis

    官网命令(英文)列表:http://redis.io/commands Redis 教程:http://www.redis.net.cn/tutorial/3501.html Redis 命令参考:h ...

  8. Python 操作redis 常用方法

    Python 操作redis 1.字符串 #!/usr/bin/env python # -*- coding:utf-8 -*-import redis# python 操作str class Te ...

  9. redis实例python_使用python操作redis(管道)

    一.redis连接 redis提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使用官方的语法和命令,Redis是StrictRe ...

最新文章

  1. 商丘网络推广是如何做好网站优化的关键词布局的呢?
  2. 使用apply调用函数
  3. 在线实时大数据平台Storm本地模式运行的一个小发现
  4. 清北学堂模拟赛d6t3 反击数
  5. jdkjmeter安装与环境变量配置
  6. 图像处理之三---摄像头灰度值处理
  7. 浅谈嵌入式软件的未来发展
  8. phpcms根据二级栏目列表写的三级栏目列表
  9. 2.微服务设计 --- 演化式架构师
  10. 简单的解决达梦数据库查询 dm.jdbc.driver.DmdbNClob@1064bb3e 问题
  11. centos安装open-jdk8
  12. 浩辰ICAD电气软件IDq2003i.rar
  13. PS换照片底色(三种方式)
  14. scum服务器在线玩家,SCUM服务器选择推荐 官服跟私服有什么区别
  15. 关于服务器等级的正确的是什么,腾讯天龙八部手游服务器等级限制是什么
  16. flyaway mysql_MySQL open table
  17. H.264中的SPamp;amp;SI帧技术简述
  18. WHT20无线温湿度传感器 STM8S单片机
  19. 计算机路由器交换机常用配置命令
  20. sec^3 不定积分

热门文章

  1. redis缓存雪崩,缓存穿透,缓存击穿的解决方法
  2. 2-7 StatusStrip 控件
  3. Bootstrap学习(一):Bootstrap简介
  4. 使用 .NET 平台,如何玩转 Universal Windows 应用?
  5. java rmi 规范学习
  6. CCNA12月12日战报
  7. 继英伟达、三星后,育碧也遭攻击,员工密码重置
  8. 2022,虎年大吉!
  9. 漏洞10年深藏不露,PHP 项目依赖关系管理工具Composer安全吗?
  10. 【日常水题-bfs】马的遍历