Five basic types of Redis
Note: Note arrangement source: B station UP master maniac said Java
Official document: Redis is an open source (BSD licensed) in memory data structure storage system, which can be used as database, cache and message middleware.
It supports many types of data structures, such as strings, hashes, lists, sets,
sorted sets and range queries, bitmaps, hyperlogs and geospatial
Index radius query. Redis has built-in replication, Lua scripting and LRU driver events
eviction, transactions, and different levels of disk persistence, through
Redis Sentinel and Cluster provide high availability. String (string)
String
127.0.0.1:6379> flushdb OK 127.0.0.1:6379> set key1 v1 Set value OK 127.0.0.1:6379> get key1 Get value "v1" 127.0.0.1:6379> keys * Get all key 1) "key1" 127.0.0.1:6379> exists key1 Judge a key Does it exist (integer) 1 127.0.0.1:6379> append key1 "hello" Append string if key No, equivalent to set key (integer) 7 127.0.0.1:6379> get kye1 (nil) 127.0.0.1:6379> strlen key1 obtain key Length of (integer) 7 127.0.0.1:6379> append key1 ",kuangshen" (integer) 17 127.0.0.1:6379> strlen key1 (integer) 17 127.0.0.1:6379> get key1 "v1hello,kuangshen" 127.0.0.1:6379> get views "0" 127.0.0.1:6379> incr views Automatic growth (integer) 1 127.0.0.1:6379> incr views (integer) 2 127.0.0.1:6379> get views "2" 127.0.0.1:6379> decr views Auto decrement (integer) 1 127.0.0.1:6379> get views "1" 127.0.0.1:6379> incrby views 10 Grow in steps of 10 (integer) 11 127.0.0.1:6379> get views "11" 127.0.0.1:6379> decrby views 8 Decrement in steps of 8 (integer) 3 127.0.0.1:6379> get views "3" 127.0.0.1:6379> set key1 "hello,fly" OK 127.0.0.1:6379> get key1 "hello,fly" 127.0.0.1:6379> getrange key1 0 4 String truncation "hello" 127.0.0.1:6379> getrange key1 0 -1 "hello,fly" 127.0.0.1:6379> setex key3 30 "hello" Set expiration time OK 127.0.0.1:6379> ttl key3 (integer) 26 127.0.0.1:6379> get key3 (nil) 127.0.0.1:6379> ttl key3 (integer) -2 127.0.0.1:6379> setnx mykey redis mykey No, setting succeeded (integer) 1 1 for success and 0 for failure 127.0.0.1:6379> keys * 1) "key2" 2) "key1" 3) "mykey" 127.0.0.1:6379> setnx mykey mongodb mykey Exist, setting failed (integer) 0 127.0.0.1:6379> get mykey "redis" 127.0.0.1:6379> mset key1 v1 key2 v2 key3 v3 Set multiple values OK 127.0.0.1:6379> keys * 1) "key2" 2) "key1" 3) "key3" 127.0.0.1:6379> mget key1 key2 key3 Get multiple values 1) "v1" 2) "v2" 3) "v3" msetnx key1 v1 key4 v4 (integer) 0 127.0.0.1:6379> getset db redis If not, return nil (nil) 127.0.0.1:6379> get db "redis" 127.0.0.1:6379> getset db mongodb If it exists, return the current value first, and then set it to a new value "redis" 127.0.0.1:6379> get db "mongodb" //It is interlinked with CAS structure
expand:
-----------
Object:
set user:1 { name:zhangsan , age:3 }Set up a user:1 Object value is json string to save an object
The key here is a clever design: user: {ID}: {file} so it is completely ok in redis
127.0.0.1:6379> mset user:1:name zhangsan user:1:age 17 OK 127.0.0.1:6379> mget user:1:name user:1:age 1) "zhangsan" 2) "17"
String similar usage scenarios:
value can be a number as well as a string
- counter
- count multiple units{ uid:9282329 : follow 0} can be inc uid:9282329:follow
-----------
List (basic data type)
In redis, you can play redis as stack, queue, blocking queue, etc. all list commands start with l
LPUSH RPUSH
127.0.0.1:6379> lpush list one Insert one or more values into the list header (integer) 1 127.0.0.1:6379> lpush list two (integer) 2 127.0.0.1:6379> lpush list three (integer) 3 127.0.0.1:6379> lrange list 0 -1 1) "three" 2) "two" 3) "one" 127.0.0.1:6379> lrange list 0 1 1) "three" 2) "two" 127.0.0.1:6379> rpush list 1 Put one or more values at the end of the list (integer) 7 127.0.0.1:6379> rpush list 2 (integer) 8 127.0.0.1:6379> rpush list 3 (integer) 9 127.0.0.1:6379> lrange list 0 -1 1) "three" 2) "two" 3) "one" 4) "1" 5) "2" 6) "3"
LPOP RPOP
127.0.0.1:6379 > remove an rpop list from the back "3" 127.0.0.1:6379 > remove an lpop list from the front "three"
Lindex
127.0.0.1:6379 > lindex list 1 get the value of index position "one"
Llen
127.0.0.1:6379> Llen list (integer) 7
Remove the specified value: Lrem
127.0.0.1:6379> lrem list 1 one remove 1 individual "one" (integer) 1 127.0.0.1:6379> lrange list 0 -1 1) "two" 2) "3" 3) "2" 4) "1" 5) "1" 6) "2"
ltrim trim: list
127.0.0.1:6379> lpush mylist hello1 (integer) 1 127.0.0.1:6379> lpush mylist hello2 (integer) 2 127.0.0.1:6379> lpush mylist hello3 (integer) 3 127.0.0.1:6379> ltrim mylist 1 2 OK 127.0.0.1:6379> lrange mylist 0 -1 1) "hello2" 2) "hello1"
Rpolpush removes the last element of the list and moves it to a new list
127.0.0.1:6379> lrange mylist 0 -1 1) "hello2" 2) "hello1" 127.0.0.1:6379> rpoplpush mylist myotherlist "hello1" 127.0.0.1:6379> lrange mylist 0 -1 1) "hello2" 127.0.0.1:6379> lrange myotherlist 0 -1 1) "hello1"
LSET specifies the subscript update value
127.0.0.1:6379> lset mylist 0 "hello3" OK
linsert inserts a value before or after the specified location
127.0.0.1:6379> linsert mylist before(after) hello3 "other" (integer) 2 127.0.0.1:6379> lrange mylist 0 -1 1) "other" 2) "hello3"
Summary:
List is actually a linked list. Before node after, left and right can insert values
If the key does not exist, create a new linked list
key exists, new content
If the key is removed and it is an empty linked list, it does not exist
Insert on both sides, or change the value, the most efficient! Low efficiency of operating intermediate elements
Message queuing! Message queue (Lpush Rpop) stack (Lpush lpop)
Set (set)
Value in set cannot be duplicate
Sadd ,Smember , SisMember
127.0.0.1:6379> sadd myset "hello" set Collection add element (integer) 1 127.0.0.1:6379> sadd myset "fly" (integer) 1 127.0.0.1:6379> sadd myset "love fly" (integer) 1 127.0.0.1:6379> SMEMBERS myset View assignments set All values of 1) "love fly" 2) "hello" 3) "fly" 127.0.0.1:6379> SISMEMBER myset hi see set Include this element or not,With return 1 ,0 was not returned (integer) 0 127.0.0.1:6379> SISMEMBER myset hello (integer) 1
Scard
127.0.0.1:6379> scard myset obtain set Number of in set (integer) 3
Srem
127.0.0.1:6379> srem myset hello remove set Elements of (integer) 1
Set disorder, do not repeat set, random
127.0.0.1:6379 > srandmember myset randomly extracts an element "love fly" 127.0.0.1:6379> SRANDMEMBER myset "love fly" 127.0.0.1:6379> SRANDMEMBER myset "love fly" 127.0.0.1:6379> SRANDMEMBER myset "love fly" 127.0.0.1:6379> SRANDMEMBER myset "fly" 127.0.0.1:6379 > srandmember myset 2 randomly extracts the specified number of elements 1) "love fly" 2) "fly"
Spop delete the specified key, and then delete the key!
127.0.0.1:6379 > spop myset randomly delete some elements in the set set "fly"
Smove moves a specified value to another collection
127.0.0.1:6379> smove myset newset fly If there is no target set, the elements of the first set will be deleted and the target set will not be created (integer) 0 127.0.0.1:6379> SMEMBERS newset (empty array) 127.0.0.1:6379> SMEMBERS myset 1) "love fly"
Set set set scene:
Microblog, station B, common concern (intersection)
Weibo: A user puts all the people concerned in A set set! Put fans in the collection
Common concerns, hobbies and second Friends of users A and B (recommended friends)
Digital set class:
- difference set
127.0.0.1:6379> sadd key a (integer) 1 127.0.0.1:6379> sadd key b (integer) 1 127.0.0.1:6379> sadd key c (integer) 1 127.0.0.1:6379> sadd key1 d (integer) 1 127.0.0.1:6379> sadd key1 c (integer) 1 127.0.0.1:6379> sadd key1 e (integer) 1 127.0.0.1:6379> SDIFF key key1 Difference set 1) "b" 2) "a" -intersection 127.0.0.1:6379> SINTER key key1 Find intersection 1) "c" -Union 127.0.0.1:6379> SUNION key key1 1) "b" 2) "c" 3) "d" 4) "a" 5) "e"
Hash (hash, Map set)
The storage is key - map! value is map set
Hash is not much different from String in nature. It's still a key value. Only the value value is map (key value)!
127.0.0.1:6379> hset myhash field fly set up key-map value (integer) 1 127.0.0.1:6379> hget myhash field obtain key-map value "fly" 127.0.0.1:6379> hmset myhash field fly field hello Set multiple key-map Value, repeated setting will overwrite OK 127.0.0.1:6379> hget myhash field "hello" 127.0.0.1:6379> hmget myhash field field1 Get multiple key-map value 1) "fly" 2) "hello" 127.0.0.1:6379> hgetall myhash Get all values 1) "field" field-fly Belong to one key-value 2) "fly" 3) "field1" 4) "hello" 127.0.0.1:6379> hdel myhash field1 Delete the specified one value value (integer) 1 127.0.0.1:6379> hgetall myhash 1) "field" 2) "fly" 127.0.0.1:6379> hmset myhash field1 1 field2 2 OK 127.0.0.1:6379> hlen myhash Get length (integer) 3 127.0.0.1:6379> hexists myhash field Judge whether the field is saved (integer) 1 127.0.0.1:6379> hkeys myhash Get only key value 1) "field" 2) "field1" 3) "field2" 127.0.0.1:6379> hvals myhash Get only value value 1) "fly" 2) "1" 3) "2" 127.0.0.1:6379> hincrby myhash field1 10 Grow in specified steps (integer) 11 127.0.0.1:6379> hincrby myhash field1 -1 Reduce in specified steps(A negative value is decrement) (integer) 10 127.0.0.1:6379> hgetall myhash 1) "field" 2) "fly" 3) "field1" 4) "11" 5) "field2" 6) "2" 127.0.0.1:6379> hsetnx myhash field f If it exists, the setting fails. If it does not exist, it is created(Distributed lock:success version+1) (integer) 0
hash stores changed data:
user name age, especially the storage of user information and frequently changed information
hash is more suitable for object storage and String is more suitable for String storage
Zset (ordered set: Sort Set)
A value is added based on set, set K1 V1 Zset K1 Score1 v1
Add value zadd key score (sort by score) value
127.0.0.1:6379> zadd myset 1 one (integer) 1 127.0.0.1:6379> zadd myset 2 two 3 three (integer) 2 127.0.0.1:6379> zrange myset 0 -1 1) "one" 2) "two" 3) "three"
Sort zrangebyscore key min max with places
127.0.0.1:6379 > zrangebycore salary - inf + inf WithCores 1) "gou" 2) "2000" 3) "fly" 4) "2500" 5) "lei" 6) "2600"
Zrevrange key start stop
127.0.0.1:6379> zrevrange salary 0 -1 1) "lei" 2) "gou"
Remove element Zrem
127.0.0.1:6379> zrem salary fly (integer) 1 View the number of elements Zcard 127.0.0.1:6379> zcard salary (integer) 2
Query the number of score in the specified interval zcount key min max
127.0.0.1:6379> zcount salary 0 2700 (integer) 2
Application scenario:
If you need to work, you can query the official documents!
*********************
set sorting can store class grade table and salary table sorting,
General message (1), important message (2)
Leaderboard application, TOP N test
*********************
Three special data types
geospatial (map, geographic location)
Positioning of friends, people nearby, and Realization of taxi distance
redis's Geo can calculate the location information (distance between two places, people in a few miles around)
You can query some test data
There are six commands:
Geoadd key longitude latitude member
Rule: there is no way to add two levels of the earth (North and south poles). Generally, the city data will be downloaded, and the file will be read and imported through the java program
Parameter: key (longitude, latitude, name)
Effective longitude - 85.05 to 85.05
Effective latitude - 180 to 180
127.0.0.1:6379> geoadd china:city 116.40 39.30 beijing (integer) 1 127.0.0.1:6379> geoadd china:city 121.47 31.32 shanghai (integer) 1 127.0.0.1:6379> geoadd china:city 106.50 29.53 chongqing (integer) 1 127.0.0.1:6379> geoadd china:city 114.08 22.54 shenzhen (integer) 1 127.0.0.1:6379> geoadd china:city 120.15 30.28 hangzhou (integer) 1 127.0.0.1:6379> geoadd china:city 108.94 34.26 xian (integer) 1
Geodist (GEO distinct: get the distance between given two positions)
Unit:
-m unit meter
-Km unit km
-mi unit mile
-ft in thousands
127.0.0.1:6379> geodist china:city beijing Shanghai straight line distance from Beijing to Shanghai m "1008342.6601" 127.0.0.1:6379> geodist china:city beijing Shanghai km the linear distance between Beijing and Shanghai km "1008.3427" 127.0.0.1:6379> geodist china:city beijing Chongqing km distance from Beijing to Chongqing km "1414.4224"
GeoHash (returns GeoHash representation of one or more locations)
127.0.0.1:6379> GEOHASH china:city beijing 1) "wwfz8drghe0" represents the 11 bit hash of the current city longitude and latitude string. If the two strings are closer, the closer the distance is Geopos (get longitude and latitude of the specified geographic location) 127.0.0.1:6379> geopos china:city xian 1) 1) "108.93999785184860229" 2) "34.25999964418929977" 127.0.0.1:6379> geopos china:city beijing 1) 1) "116.39999896287918091" 2) "39.30000117660147652" 127.0.0.1:6379> geopos china:city chongqing 1) 1) "106.49999767541885376" 2) "29.52999957900659211" 127.0.0.1:6379> geopos china:city shenzhen 1) 1) "114.08000081777572632" 2) "22.53999903789756587" 127.0.0.1:6379> geopos china:city hangzhou 1) 1) "120.15000075101852417" 2) "30.2800007575645509" 127.0.0.1:6379>
Geordius (find out the elements within a certain radius with the given longitude and latitude as the center)
Wechat to find people near your friends? (get the address of nearby people, locate) query by radius!
127.0.0.1:6379> GEORADIUS china:city 110 Within a radius of 1000 km and with 110 30 as the center 1) "chongqing" 2) "shenzhen" 3) "hangzhou" Get the designated number of people 200 127.0.0.1:6379> GEORADIUS china:city 110 Get 1 person from 30 1000 km withcoord withdist count 1 1) 1) "chongqing" 2) "341.9374" 3) 1) "106.49999767541885376" 2) "29.52999957900659211"
All data should be entered china:city In order to make the result more accurate
Location between cities (navigation)
127.0.0.1:6379> GEORADIUSBYMEMBER china:city beijing 1000 km 1) "beijing" 2) "xian" 127.0.0.1:6379> GEORADIUSBYMEMBER china:city shanghai 400 km 1) "hangzhou" 2) "shanghai"
The implementation principle of GEO bottom layer is Zset!
127.0.0.1:6379> type china:city
zset
You can use the Zset command to operate Geo
127.0.0.1:6379> zrange china:city 0 -1 View all elements of the map 1) "chongqing" 2) "xian" 3) "shenzhen" 4) "hangzhou" 5) "shanghai" 6) "beijing" 127.0.0.1:6379> zrem china:city shenzhen Delete specified map elements (integer) 1 127.0.0.1:6379> zrange china:city 0 -1 1) "chongqing" 2) "xian" 3) "hangzhou" 4) "shanghai" 5) "beijing"
**
hyperloglog
What is the cardinality?
A{1,3,5,7,5,9}
B{1,3,5,7,8}
Cardinality (non repeating elements): introduction to redis 2.8.9 updates the Hyperloglog data structure! Redis Hyperloglog cardinality statistics algorithm!
Advantages: the memory occupied is fixed, 2 ^ 64 different elements are non-technical, only 12KB memory is needed. From the perspective of memory, hyperlog is the preferred UV(Uniqe) for web pages
Visitor: the number of independent visitors who visit a website many times is counted as one person)
The traditional way: set saves the user's id, and then counts the number of elements in the set as the standard judgment
If this method saves a large number of user IDs and takes up too much memory, it will be very troublesome! The purpose is to count, not to save the id
0.81% error rate! The task of UV statistics is negligible
Test use
127.0.0.1:6379> pfadd mykey a b c d e f g h i j Create the first set of elements (integer) 1 127.0.0.1:6379> pfcount mykey Count the cardinality number of the first group of elements (integer) 10 127.0.0.1:6379> pfadd mykey2 i j z x c v b n m (integer) 1 127.0.0.1:6379> pfcount mykey2 (integer) 9 127.0.0.1:6379> PFMERGE mykey3 mykey2 mykey Merge two groups mykey + mykey2 ->mykey3 OK 127.0.0.1:6379> pfcount mykey3 (integer) 15
Application scenario
Make page statistics
If fault tolerance is allowed, Hyperloglog must be used
Fault tolerance is not allowed. Just use set or your own data type!
Bitmap (bitmap)
Bit storage: all binary bits are used for recording. If it is not 0, the number of infected persons will be counted. If it is not 0, the number of infected persons will be counted. If it is 1, the user information will be counted,
Number of active or inactive people
365 days = 365bit 1 byte = 8bit 46 bytes can be stored (save memory)
As long as there are two states, you can use bitmap
Use bitmaps to record the clock in setbit key offer value from Monday to Sunday
Monday: 1 Tuesday: 1 Wednesday: 0
127.0.0.1:6379> setbit sign 0 1 (integer) 0 127.0.0.1:6379> setbit sign 1 1 (integer) 0 127.0.0.1:6379> setbit sign 2 0 (integer) 0 127.0.0.1:6379> setbit sign 3 0 (integer) 0 127.0.0.1:6379> setbit sign 4 1 (integer) 0 127.0.0.1:6379> setbit sign 5 1 (integer) 0 127.0.0.1:6379> setbit sign 6 1 (integer) 0
Check whether there is a punch in getbit key offer one day
127.0.0.1:6379> getbit sign 1 (integer) 1 127.0.0.1:6379> getbit sign 3 (integer) 0 //Count the number of days to punch bitcount key [start end] 127.0.0.1:6379> bitcount sign 0 6 (integer) 5
Redis transaction operation
What is a transaction: a collection of commands executed together
All commands in a transaction will be serialized and executed in order when the transaction is executed! One off, sequential, exclusive
---------------Queue set set set execution----------------
Either succeed at the same time, or fail at the same time, atomicity
redis transaction does not guarantee atomicity!!!
redis single command is atomic!!!
redis transaction has no isolation level concept! (no unreal reading, dirty reading, non repeatable reading...)
All commands are not executed directly in the transaction! Only when the execution command is initiated can it be executed!
redis transaction:
- open transaction (multi)
- order to join ()
- execute transaction (exec)
Normal transaction execution:
127.0.0.1:6379> multi Open transaction OK 127.0.0.1:6379> set key1 v1 QUEUED 127.0.0.1:6379> set key2 v2 QUEUED 127.0.0.1:6379> set key3 v3 QUEUED 127.0.0.1:6379> get key2 QUEUED 127.0.0.1:6379> get key1 QUEUED 127.0.0.1:6379> exec Execute transaction 1) OK 2) OK 3) OK 4) "v2" 5) "v1" 127.0.0.1:6379> multi Open transaction OK 127.0.0.1:6379> set key1 v1 QUEUED 127.0.0.1:6379> set key2 v2 QUEUED 127.0.0.1:6379> set key4 v4 QUEUED 127.0.0.1:6379> DISCARD Cancel transaction OK 127.0.0.1:6379> get key4 Commands in the transaction queue will not be executed (nil)
Compile exception (redis command error): all commands in the transaction will not be executed
127.0.0.1:6379> multi OK 127.0.0.1:6379> set k1 v1 QUEUED 127.0.0.1:6379> set k2 v2 QUEUED 127.0.0.1:6379> getset k3 v3 QUEUED 127.0.0.1:6379> getset k4 (error) ERR wrong number of arguments for 'getset' command 127.0.0.1:6379> set k5 v5 QUEUED 127.0.0.1:6379> exec (error) EXECABORT Transaction discarded because of previous errors. 127.0.0.1:6379> get v5 All commands will not be executed (nil)
Runtime exception (syntax error): there is a syntax problem in the transaction. Other commands can be executed normally when executing,
Bad command throw exception (so no atomicity)
127.0.0.1:6379> multi OK 127.0.0.1:6379> set k2 "v2" QUEUED 127.0.0.1:6379> incr k2 QUEUED 127.0.0.1:6379> exec 1) OK 2) (error) ERR value is not an integer or out of range
Lock: Redis can realize optimistic lock and watch monitoring!
Pessimistic lock: very pessimistic, think that whenever there will be problems, no matter what you do will be locked! Affect performance
Optimistic lock: very optimistic. I don't think there will be any problem, so I won't lock it! When updating data, judge whether someone has modified the data during this period
1. get version
2. Compare version when updating
Once the monitoring test of Redis succeeds, the monitoring will be cancelled
Normal execution successful: single thread
127.0.0.1:6379> set money 100 OK 127.0.0.1:6379> set out 0 OK 127.0.0.1:6379> watch money monitor money object OK 127.0.0.1:6379> multi The transaction ends normally, and there is no change in the data period OK 127.0.0.1:6379> decrby money 20 QUEUED 127.0.0.1:6379> incrby out 20 QUEUED 127.0.0.1:6379> exec 1) (integer) 80 2) (integer) 20 127.0.0.1:6379> mget money out 1) "80" 2) "20"
Multithreading: multiple client operations
127.0.0.1:6379> watch money OK 127.0.0.1:6379> multi OK 127.0.0.1:6379> decrby money 10 QUEUED 127.0.0.1:6379> incrby out 10 QUEUED //Thread 2 came in and modified money 127.0.0.1:6379> get money "80" 127.0.0.1:6379> set money 1000 OK 127.0.0.1:6379> exec Commit transaction discovery meney If it is modified, it will fail (nil) //If the transaction fails to execute, you need to unlock it first, and then get the latest value 127.0.0.1:6379> unwatch 1.Discover transaction execution failure, unlock first OK 127.0.0.1:6379> watch money 2.Get the latest value, monitor again, select version OK 127.0.0.1:6379> multi OK 127.0.0.1:6379> decrby money 80 QUEUED 127.0.0.1:6379> incrby out 80 QUEUED 127.0.0.1:6379> exec 3.Before each execution, check whether the monitored value has changed 1) (integer) 920 2) (integer) 100
watch can be used as optimistic lock operation of redis!
Jedis operation redis
Using java to operate redis
What is jedis?
Is the official recommendation of Java connection development tools! Using java to operate redis
Step 1:
<dependencies <!-- Import jedis 's bag-->> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.2.0</version> </dependency> <!--Import fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency> </dependencies>
2. Coding test:
- connect to database
- operation command
- disconnect
public class TestPing { public static void main(String[] args) { //1. Create a Jedis object Jedis jedis = new Jedis("127.0.0.1",6379); //All the commands of Jedis are those of redis //Operation String hash set ZSet list geo hyperloglog bitmap System.out.println(jedis.ping()); } }
java common API for five data types
Operate redis keys
Jedis jedis = new Jedis("127.0.0.1", 6379); System.out.println("wipe data :"+jedis.flushDB()); System.out.println("Determine whether a value exists:"+jedis.exists("username")); System.out.println("newly added<username , fly>Key value pair of:"+jedis.set("username","fly")); System.out.println("newly added<password , 123>Key value pair of:"+jedis.set("password","123")); System.out.print("All keys of the system are as follows:"); Set<String> keys = jedis.keys("*"); System.out.println("All keys:"+keys); System.out.println("Delete key password:"+jedis.del("password")); System.out.println("Judgment key password Does it exist:"+jedis.exists("password")); System.out.println("see username Stored value type:"+jedis.type("username")); System.out.println("Random return key A value of space:"+jedis.randomKey()); System.out.println("rename key:"+jedis.rename("username","name")); System.out.println("Extract value:"+jedis.get("name")); System.out.println("Query by index:"+jedis.select(0)); System.out.println("Delete current database all key value:"+jedis.flushDB()); System.out.println("Return to current database key Number of values:"+jedis.dbSize()); System.out.println("Delete the key value:"+jedis.flushAll());
Sring:
Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.flushDB(); System.out.println("----Add data----"); System.out.println(jedis.set("key1","value1")); System.out.println(jedis.set("key2","value2")); System.out.println(jedis.set("key3","value3")); System.out.println("delete key2:"+jedis.del("key2")); System.out.println("obtain key2:"+jedis.get("key2")); System.out.println("modify key1 Value of:"+jedis.set("key1","afterV1")); System.out.println("obtain key1 Value of:"+jedis.get("key1")); System.out.println("stay key3 Add value after:"+jedis.append("key3",",hello")); System.out.println("key3 Value of:"+jedis.get("key3")); System.out.println("Add multiple key-value value:"+jedis.mset("k1","v1","k2","v2","k3","v3")); System.out.println("Get multiple key-value value:"+jedis.mget("k1","key1","k2","k3")); System.out.println("Delete multiple values:"+jedis.del("k1","k2")); System.out.println("Get multiple values:"+jedis.mget("username","name","k3")); jedis.flushDB(); System.out.println("----New value added prevention coverage----"); System.out.println(jedis.setnx("key1","value1")); System.out.println(jedis.setnx("key2","value2")); System.out.println(jedis.setnx("key2","value2-new")); System.out.println(jedis.get("key1")); System.out.println(jedis.get("key2")); System.out.println("---Add value and set effective time---"); System.out.println(jedis.setex("key3",10,"count down")); System.out.println(jedis.get("key3")); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(jedis.get("key3")); System.out.println("---Get the original value and update it to the new value---"); System.out.println(jedis.getSet("key1", "newkey1")); System.out.println(jedis.get("key1")); System.out.println("obtain key2 String of:"+jedis.getrange("key2",2,4)); System.out.println("Delete the key value:"+jedis.flushAll());
List:
Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.flushDB(); System.out.println("add to list element:"+jedis.lpush("Collection","List","Map","Vector")); System.out.println("add to list element:"+jedis.lpush("Collection","HashMap")); System.out.println("add to list element:"+jedis.lpush("Collection","TreeSet")); System.out.println("Collection Content of(whole):"+jedis.lrange("Collection",0,-1)); System.out.println("Collection Content of[0,3]:"+jedis.lrange("Collection",0,3)); System.out.println("-----Delete the value specified in the list-----"); System.out.println("Delete the specified number of elements:"+jedis.lrem("Collection",2,"HashMap")); System.out.println("Delete the following table[0,3]Value in interval:"+jedis.ltrim("Collection",0,3)); System.out.println("Stack from the left side of the list:"+jedis.lpop("Collection")); System.out.println("Add value from right side of list:"+jedis.rpush("Collection","iterator")); System.out.println("Stack from the right end of the list:"+jedis.rpop("Collection")); System.out.println("Get the content of the specified subscript:"+jedis.lindex("Collection",2)); System.out.println("modify Collection Value with subscript 1"+jedis.lset("Collection",1,"ArrayList")); System.out.println("------------------------"); System.out.println("Collection Length of:"+jedis.llen("Collection")); System.out.println("obtain Collection Value with subscript 2:"+jedis.lindex("Collection",2)); System.out.println("------------------------"); System.out.println("SortList:"+jedis.lpush("sortList","3","6","1","2","7","4")); System.out.println("sortList sort(from small to large)Before sorting:"+jedis.lrange("sortList",0,-1)); System.out.println("sortList sort(from small to large)After sorting:"+jedis.sort("sortList"));
Set:
Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.flushDB(); System.out.println("Add elements to the collection------"); System.out.println(jedis.sadd("eleSet", "e1", "e2", "e3", "e4", "e5", "e6")); System.out.println("Set All elements in are:"+jedis.smembers("eleSet")); System.out.println("Delete an element e1:"+jedis.srem("eleSet","e1")); System.out.println("Set All elements in are:"+jedis.smembers("eleSet")); System.out.println("Delete 2 elements e3,e4:"+jedis.srem("eleSet","e3","e4")); System.out.println("Set All elements in are:"+jedis.smembers("eleSet")); System.out.println("Randomly remove an element from a collection:"+jedis.spop("eleSet")); System.out.println("Number of elements in the collection:"+jedis.scard("eleSet")); System.out.println("e1 Is there a collection:"+jedis.sismember("eleSet","e1")); System.out.println("e2 Is there a collection:"+jedis.sismember("eleSet","e2")); System.out.println("---------------------"); System.out.println(jedis.sadd("eleSet1","e1","e2", "e3", "e4", "e5", "e6")); System.out.println(jedis.sadd("eleSet2","e1","e0", "e9", "e6")); System.out.println("delete eleSet1 Of e2,And move to eleSet2 in:"+jedis.smove("eleSet1","eleSet2","e2")); System.out.println("eleSet1 Elements in:"+jedis.smembers("eleSet1")); System.out.println("eleSet2 Elements in:"+jedis.smembers("eleSet2")); System.out.println("----------Set operation--------"); System.out.println("eleSet1 and eleSet2 Frontal intersection:"+jedis.sinter("eleSet1","eleSet2")); System.out.println("eleSet1 and eleSet2 Frontal Union:"+jedis.sunion("eleSet1","eleSet2")); System.out.println("eleSet1 and eleSet2 Frontal difference set:"+jedis.sdiff("eleSet1","eleSet2")); jedis.sinterstore("eleSet3","eleSet1","eleSet2"); System.out.println("eleSet3 Value of:"+jedis.smembers("eleSet3"));
Hash:
Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.flushDB(); Map<String,String> map = new HashMap<String,String>(); map.put("key1","value1"); map.put("key2","value2"); map.put("key3","value3"); map.put("key4","value4"); //Add a hash element with the name (key) hash jedis.hset("hash",map); jedis.hset("hash","key5","value5"); System.out.println("hash All key value pairs are:"+jedis.hgetAll("hash")); System.out.println("hash All keys for are:"+jedis.hkeys("hash")); System.out.println("hash All values of are:"+jedis.hvals("hash")); System.out.println("take key66 Save the value plus an integer. If not, create key6:"+jedis.hincrBy("hash","key6",1)); System.out.println("hash All key value pairs are:"+jedis.hgetAll("hash")); System.out.println("Delete one or more key value pairs:"+jedis.hdel("hash","key1","key6")); System.out.println("hash All key value pairs are:"+jedis.hgetAll("hash")); System.out.println("hash The number of all key value pairs is:"+jedis.hlen("hash")); System.out.println("judge hash Does it exist key2:"+jedis.hexists("hash","key2")); System.out.println("judge hash Does it exist key3:"+jedis.hexists("hash","key3")); System.out.println("obtain hash Value in:"+jedis.hmget("hash","key3")); System.out.println("obtain hash Value in:"+jedis.hmget("hash","key3","key4"));