Redis学习笔记
LPUSH puts the new value at the start of the list.
LPUSH friends “Sam”
LPOP、RPOP为移除列表的第一个/最后一个元素。
LRANGE gives a subset of the list. It takes the index of the first element you want to retrieve as its first parameter and the index of the last element you want to retrieve as its second parameter. A value of -1 for the second parameter means to retrieve elements until the end of the list.
LRANGE friends 0 -1 = 1) "Sam", 2) "Alice", 3) "Bob" LRANGE friends 0 1 = 1) "Sam", 2) "Alice" LRANGE friends 1 2 = 1) "Alice", 2) “Bob"
LLEN returns the current length of the list.
LLEN friends = 3
LPOP removes the first element from the list and returns it.
LPOP friends = "Sam"
RPOP removes the last element from the list and returns it.
RPOP friends = "Bob"
Note that the list now only has one element:
LLEN friends = 1 LRANGE friends 0 -1 = 1) "Alice"4 set
SADD adds the given value to the set.
SADD superpowers "flight" SADD superpowers "x-ray vision" SADD superpowers "reflexes"
SREM removes the given value from the set.
SREM superpowers “reflexes" SCARD myset = 4 (set中元素个数) SINTER //返回两个或多个set的交集
SISMEMBER tests if the given value is in the set. It returns 1 if the value is there and 0 if it is not.
SISMEMBER superpowers "flight" = 1 SISMEMBER superpowers "reflexes" = 0
SMEMBERS returns a list of all the members of this set.
SMEMBERS superpowers = 1) "flight", 2) "x-ray vision"
SUNION combines two or more sets and returns the list of all elements.
SADD birdpowers "pecking" SADD birdpowers "flight" SUNION superpowers birdpowers = 1) "pecking", 2) "x-ray vision", 3) “flight” SPOP //移除并返回列表中的一个随机元素 ZCOUNT //计算在有序集合中指定区间分数的成员数5 zset
Sets are a very handy data type, but as they are unsorted they don’t work well for a number of problems. This is why Redis 1.2 introduced Sorted Sets.
A sorted set is similar to a regular set, but now each value has an associated score. This score is used to sort the elements in the set.
ZADD hackers 1940 "Alan Kay" ZADD hackers 1906 "Grace Hopper" ZADD hackers 1953 "Richard Stallman" ZADD hackers 1965 "Yukihiro Matsumoto" ZADD hackers 1916 "Claude Shannon" ZADD hackers 1969 "Linus Torvalds" ZADD hackers 1957 "Sophie Wilson" ZADD hackers 1912 "Alan Turing"
In these examples, the scores are years of birth and the values are the names of famous hackers.
ZRANGE hackers 2 4 = 1) "Claude Shannon", 2) "Alan Kay", 3) "Richard Stallman” ZSCORE //获取元素的score值6 hset
Simple strings, sets and sorted sets already get a lot done but there is one more data type Redis can handle: Hashes.
Hashes are maps between string fields and string values, so they are the perfect data type to represent objects (eg: A User with a number of fields like name, surname, age, and so forth):
HSET user:1000 name "John Smith" HSET user:1000 email "john.smith@example.com" HSET user:1000 password "s3cret"
To get back the saved data use HGETALL:
HGETALL user:1000
You can also set multiple fields at once:
HMSET user:1001 name "Mary Jones" password "hidden" email "mjones@example.com"
If you only need a single field value that is possible as well:
HGET user:1001 name = "Mary Jones”PUBSUB
redis是一个基于key-value的存储系统,但同样提供了许多其他的使用的组件,比如SUBSCRIBE、PUBLISH这两个命令。这两个命令能让你非常方便地再两个进程中进行通信。
订阅/发布/取消订阅
SUBSCRIBE、PUBLISH、UNSUBSCRIBE
# SUBSCRIBE 订阅给定的一个或多个频道的信息 SUBSCRIBE channel1 channel2 127.0.0.1:6379 SUBSCRIBE channel1 Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "channel1" 3) (integer) 1 # PUBLISH 将信息发送到指定的频道 PUBLISH CHANNEL MESSAGE 返回值为订阅该频道的客户端数量 127.0.0.1:6379 PUBLISH channel1 message1 (integer) 1
Redis事务让一组命令在单个步骤执行。事务中有两个属性,说明如下:
在一个事务中的所有命令按顺序执行作为单个隔离操作。通过另一个客户端发出的请求在Redis的事务的过程中执行,这是不可能的。
Redis的事务具有原子性。原子意味着要么所有的命令都执行或都不执行。
Redis的事务由指令多重发起,然后需要传递在事务,而且整个事务是通过执行命令EXEC执行命令列表。
redis 127.0.0.1:6379 MULTI List of commands here redis 127.0.0.1:6379 EXECJava连接Redis
首先需要下载jedis.jar,将其加入类路径,或者添加maven依赖。
dependency groupId redis.clients /groupId artifactId jedis /artifactId version 2.8.1 /version /dependency
代码示例如下:
import redis.clients.jedis.Jedis; import java.util.List; import java.util.Map; public class TestRedis { public static void main(String[] args) { Jedis jedis = new Jedis("localhost"); print(jedis.ping()); print(); jedis.set("name", "lee"); print(jedis.get("name")); print(); jedis.lpush("vector", "bbb"); jedis.lpush("vector", "aaa"); jedis.rpush("vector", "ccc"); List String list = jedis.lrange("vector", 0, -1); for (int i = 0; i list.size(); i++) { print(list.get(i)); print(); jedis.hset("person1", "name", "Lee"); jedis.hset("person1", "age", "12"); jedis.hset("person1", "address", "beijing"); Map String, String mymap = jedis.hgetAll("person1"); for (Map.Entry String, String entry : mymap.entrySet()) { print(entry.getKey() + " " + entry.getValue()); public static void print(Object...objects) { for (Object obj: objects) { System.out.print(obj.toString() + " "); System.out.println(); }
转载:http://blog.csdn.net/foreverling/article/details/50960640
【Redis入门】在阿里云上快速安装 Redis 如果你最近打算学习 redis 并且买了阿里云的 Linux 服务器,那么借助阿里云服务器和宝塔Linux面板,只需要简单几步就可以安装好 redis。
Redis学习笔记(二) 五、Redis集合(set) 5.1、简介 5.2、常用命令 5.3、简单测试 六、Redis 哈希(Hash) 6.1、简介 6.2、常用命令 6.3、简单测试 七、Redis有序集合Zset 7.1、简介 7.2、常用命令 7.3、简单测试
相关文章
- 警惕:Redis服务器漏洞攻击危机”(redis漏洞攻击)
- 精通 Redis 语法:一步一步学习掌握(redis语法)
- Redis缓存:失效的灾难(redis缓存失效)
- 快速学习:如何连接使用 Redis?(redis怎么连接)
- 利用Redis提升图片文件缓存性能(redis缓存图片文件)
- 最强菜鸟:使用Redis极速学习(菜鸟教程redis)
- 优雅监控Redis:基于Redis Inspector工具(redis监控工具)
- 线上 Redis 培训,学习新技术!(redis培训)
- 虚拟机上简单快捷安装Redis教程(虚拟机安装redis教程)
- 虚拟主机支持Redis服务加速你的网站(虚拟主机支持redis吗)
- 最佳实践获取Redis集群的精准时间(获取redis集群时间)
- 学会看懂Redis源码 尽享学习好收获(看懂redis源码好处)
- 使用Redis集合存储对象信息(用redis集合存对象)
- 新手如何学习查看Redis Key值(查看redis key值)
- 重视Redis服务器,持之以恒清理缓存(清redis服务器缓存)
- 学习Redis利用其魅力掌控数据(有关redis的电子书)
- Redis文档极致的学习体验(最好的redis文档)
- Redis学习如何使用值类型(下列关于redis值类型)
- 学习若依是否必须掌握Redis(学若依必须会redis吗)
- Redis入门与实战学习Redis必备之书(学redis推荐书籍)
- 如何让Redis支持开机自启动(如何让redis开机自启)
- 学习Redis多线程,深入理解和实践(怎么理解redis多线程)
- 登录基于Redis实现高效安全的单点登录(基于redis的单点)
- 后端程序员,你该不该学习Redis(后端需要学redis吗)
- 千峰Redis笔记掌握NoSQL新技术(千峰redis笔记)
- 学习Redis,可助我们实现更强更快的技能(redis需要学吗)
- Redis集群中的连接数计算法则(redis集群连接数计算)
- Redis设计注意事项预防应用性能下降(redis 设计注意事项)
- 红色的训练学习Redis的视频指南(redis训练视频)
- 权限管理基于Redis实现角色资源权限管理(redis 角色资源)
- 利用Redis简单高效实现缓存推荐排序(redis 缓存推荐排序)