Redis is the industry's most widely used memory data storage.
Redis supports rich data structures, while supporting data persistence.
Redis properties also provide some type of database, such as transaction, HA, from the master library.
REmote DIctionary Server (Redis) is written by Salvatore Sanfilippo of key-value storage systems.
Redis supports data persistence, you can save data in memory to disk, reboot to load when you can use again
Redis only supports simple key-value data type, and also supports storage list, set, zset, hash data structures, etc.
Redis supports backup data
Supports master-slave synchronization, the data stored in memory, superior performance.
Redis data structures
String (String)
String is redis basic type, a key corresponding to a value
1 2 3 4
127.0.0.1:6379> set name "runoob" OK 127.0.0.1:6379> get name "runoob"
set and get commands, key for the name, value is runoob.
List (list)
Bi-list, for the latest list, watchlist
List is a simple list of strings, sort insertion order. You can add an element to the head or tail of the list
1 2 3 4 5 6 7 8 9 10
127.0.0.1:6379> lpush runoob redis (integer) 1 127.0.0.1:6379> lpush runoob mongodb (integer) 2 127.0.0.1:6379> lpush runoob damin (integer) 3 127.0.0.1:6379> lrange runoob 0 10 1) "damin" 2) "mongodb" 3) "redis"
lpush into the list, lrange list list
Set (collection)
Suitable for unordered collection, stepped thumbs point, draw, read, common friends
Redis of type String Set is an unordered collection.
Collection is achieved through a hash table, so add, delete, search, is $ O (1) $
$ Asdd $ command: add a string element of the set command to set the corresponding key, the successful return 1 if the element exists, returns 0, key corresponding set does not exist return an error.
1 2 3 4 5 6 7 8 9 10 11 12
127.0.0.1:6379> SADD damin redis (integer) 1 127.0.0.1:6379> SADD damin mysql (integer) 1 127.0.0.1:6379> SADD damin mongodb (integer) 1 127.0.0.1:6379> SADD damin mysql (integer) 0 127.0.0.1:6379> SMEMBERS damin 1) "mongodb" 2) "mysql" 3) "redis"
SortedSet (ordered set)
List, priority queue
And set the same, zadd is not allowed to repeat a member
Hash (hash)
Object properties, the number of variable length property
It is a key name for the collection
hash particularly suitable for storing objects
1 2 3 4 5 6 7 8 9
127.0.0.1:6379> HMSET user:1 username runoob password runoob points 200 OK 127.0.0.1:6379> HGETALL user:1 1) "username" 2) "runoob" 3) "password" 4) "runoob" 5) "points" 6) "200"
Redis HMSET, HGETALL command, user: 1 is the key
KV: single values, codes, PV, cache
Start Redis
1 2
1, with the cmd command to the root directory redis 2 Execute cmd: redis-server redis.windows.conf
Redis basis
The basic command functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
public class implements InitializingBean { private static final Logger logger = LoggerFactory.getLogger(JedisAdapter.class); private JedisPool pool; public static void print (int index, Object obj) { System.out.println(String.format("%d, %s" , index, obj.toString())); } public static void main (String[] argv) { Jedis jedis = new Jedis("redis://localhost:6379/9" ); jedis.flushDB(); jedis.set("hello" , "world" ); print(1 , jedis.get("hello" )); jedis.rename("hello" , "newhello" ); print(1 , jedis.get("newhello" )); jedis.setex("hello2" , 1800 , "world" ); print(1 ,jedis.get("hello2" )); jedis.set("pv" ,"100" ); jedis.incr("pv" ); jedis.incrBy("pv" ,5 ); print(2 ,jedis.get("pv" )); jedis.decrBy("pv" ,2 ); print(2 ,jedis.get("pv" )); Print ( . 3 , jedis.keys ( "*" )); ListName = String "List" ; jedis.del (listName); for ( int I = 0 ; I < 10 ; I ++) { jedis.lpush (listName, "A" + String.valueOf (I)); } Print ( . 4 , jedis.lrange (listName, 0 , 12 is )); Print ( . 4 , jedis.lrange (listName, 0 , 2 )); Print ( . 5 , jedis.llen (listName)); Print ( . 6 , jedis.lpop (listName)); Print ( 10 , jedis.linsert (listName, BinaryClient.LIST_POSITION.AFTER, "A4" , "dd" )); Print ( . 11 , jedis.lrange ( listName, 0 , . 11 )); String userKey = "userxx" ; jedis.hset(userKey, "name" , "jim" ); jedis.hset(userKey, "age" , "12" ); jedis.hset(userKey, "phone" , "1562225555" ); print( 12 ,jedis.hget(userKey, "name" )); print( 13 ,jedis.hgetAll(userKey)); jedis.hdel(userKey, "phone" ); print( 14 ,jedis.hgetAll(userKey)); 15 Print ( , jedis.hexists (UserKey, "In Email" )); Print ( 16 , jedis.hexists (UserKey, "Age" )); Print ( . 17 , jedis.hkeys (UserKey)) ; Print ( 18 is , jedis.hvals (UserKey)); jedis.hsetnx (UserKey, "school" , "ZJU" ); jedis.hsetnx (UserKey, "name" , "YXY" ); Print ( . 19 , jedis.hgetAll (UserKey)); String likeKey1="commentLike1" ; String likeKey2="commentLike2" ; for (int i=0 ;i<10 ; ++i){ jedis.sadd(likeKey1,String.valueOf(i)); jedis.sadd(likeKey2,String.valueOf(i*i)); } print(20 ,jedis.smembers(likeKey1)); print(21 ,jedis.smembers(likeKey2)); print(22 ,jedis.sunion(likeKey1,likeKey2)); print(23 ,jedis.sdiff(likeKey1,likeKey2)); print(24 , jedis.sinter (likeKey1, likeKey2)); Print ( 25 , jedis.sismember (likeKey1, "12 is" )); Print ( 25 , jedis.sismember (likeKey2, "16" )); jedis.srem (likeKey1, "5" ); Print ( 27 large columns Spring Boot of Redis Detailed , jedis.smembers (likeKey1)); User user = new User(); user.setName("xx" ); user.setPassword("ppp" ); user.setHeadUrl("a.png" ); user.setSalt("salt" ); user.setId(1 ); print(46 ,JSONObject.toJSONString(user)); jedis.set("user1" ,JSONObject.toJSONString(user)); String value = jedis.get("user1" ); User user2=JSON.parseObject(value,User.class); print(47 ,user2); int k=2 ; } @Override public void afterPropertiesSet () throws Exception { } }
Redis Affairs
redis transaction can execute multiple commands once, and have the following two properties:
A transaction is a separate isolated operation, all of the transaction commands are serialized, in the execution order, the execution of the transaction, the command will not be sent by the other client interrupt request
A transaction is an atomic operation, either all of the commands in the transaction is performed, or not performed
A transaction from start to execution has three phases:
Begin transaction
Command into the team
The enforcement branch
Example:
MULTI first to start a transaction, and then multiple commands into teams to the transaction, the transaction is triggered by the last EXEC command, together with all the commands in the transaction
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
127.0.0.1:6379> MULTI OK 127.0.0.1:6379> set book-name "Java Thinking in" QUEUED 127.0.0.1:6379> GET book-name QUEUED 127.0.0.1:6379> SADD tag "JAVA" "Coding" QUEUED 127.0.0.1:6379> SMEMBERS tag QUEUED 127.0.0.1:6379> EXEC 1) OK 2) "Java Thinking in" 3) (integer) 2 4) 1) "JAVA" 2) "Coding"
事务相关命令:
DISCARD:取消事务discard,放弃执行事务中所有命令
EXEC:执行所有事务块内的命令 exec
MULTI:标记一个事务块的开始multi
UNWATCH:取消watch命令对所有key的监视
Redis脚本
Redis脚本用Lua解释器来执行脚本,脚本执行的命令为EVAL
1 2 3 4 5
127.0.0.1:6379> EVAL "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 key1 key2 first second 1) "key1" 2) "key2" 3) "first" 4) "second"
Redis 连接
Redis连接服务器
1 2 3 4 5
127.0.0.1:6379> AUTH "password" (error) ERR Client sent AUTH, but no password is set //没有密码,所以... 127.0 .0 .1 :6379 > pingPONG 127.0 .0 .1 :6379 >
连接命令:
AUTH password:验证密码是否正确
PONG:查看服务器是否运行
ECHO message : 打印字符串
QUIT:关闭当前连接
SELECT index :切换到指定的数据库
Java使用Redis
添加jedis.jar 包
然后就可以嗨起来了
连接服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import redis.clients.jedis.Jedis;public class RedisJava { public static void main (String [] args) { Jedis jedis =new Jedis("localhost" ); System.out.println("连接成功" ); System.out.println("服务正在运行:" +jedis.ping()); } } --- 连接成功 服务正在运行:PONG
字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import redis.clients.jedis.Jedis;public class RedisJava { public static void main (String [] args) { Jedis jedis =new Jedis("localhost" ); System.out.println("连接成功" ); jedis.set("damin" ,"zhoudm.com" ); System.out.println("redis 存储的字符串为:" +jedis.get("damin" )); } } --- 连接成功 redis 存储的字符串为:zhoudm.com
Redis Java List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
import redis.clients.jedis.Jedis;import java.util.List;public class RedisJava { public static void main (String [] args) { Jedis jedis =new Jedis("localhost" ); System.out.println("连接成功" ); jedis.lpush("zhoudm" ,"zhoudm.com" ); jedis.lpush("zhoudm" ,"baidu.com" ); jedis.lpush("zhoudm" ,"360.com" ); jedis.lpush("zhoudm" ,"google.com" ); List<String> list=jedis.lrange("zhoudm" ,0 ,3 ); for (int i=0 ;i<list.size();i++) { System.out.println("列表为:" + list.get(i)); } } } --- 连接成功 列表为:google.com 列表为:360 .com 列表为:baidu.com 列表为:zhoudm.com
Redis Java Keys
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
import redis.clients.jedis.Jedis;import java.util.Set;import java.util.Iterator;public class RedisJava { public static void main (String [] args) { Jedis jedis =new Jedis("localhost" ); System.out.println("连接成功" ); Set<String> keys=jedis.keys("*" ); Iterator<String> it=keys.iterator(); while (it.hasNext()){ String key = it.next(); System.out.println(key); } } } --- 连接成功 zhoudm spring:session:sessions:7f f2fe7e-9454 -48 a1-b44c-9 b5f6b7b1507 book-name spring:session:sessions:9 a02f60c-fd16-49 b5-9977 -08446f 4ffff5 spring:session:sessions:f1312270-95 cf-4 d19-a88d-5 af26b03a61c spring:session:sessions:expires:9 a02f60c-fd16-49 b5-9977 -08446f 4ffff5 spring:session:expirations:1504881480000 user:1 spring:session:sessions:f31624a8-b014-4768 -826 d-8 b5b484b63a7 collector spring:session:sessions:expires:f1312270-95 cf-4 d19-a88d-5 af26b03a61c spring:session:sessions:expires:f31624a8-b014-4768 -826 d-8 b5b484b63a7 name spring:session:expirations:1504409460000 damin runoob tag spring:session:expirations:1505295120000 spring:session:expirations:1504252440000 spring:session:sessions:expires:7f f2fe7e-9454 -48 a1-b44c-9 b5f6b7b1507