- Redis is an open source, advanced key-value store that can store different data structures like strings, hashes, lists, sets and sorted sets.
- Jedis is the recommended Redis client library for Java. It can be used to connect to Redis and execute commands to set, get, and manipulate Redis data structures.
- The document provides examples of common Redis commands to work with strings, lists, sets, sorted sets, hashes, publish/subscribe and more using the Jedis client.
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Intro to Redis
1.
2. What is Redis?
• Redis is an open source, advanced key-value store. It is often referred to as a data
structure server since keys can contain strings, hashes, lists, sets and sorted sets.
http://redis.io
3. What is Jedis?
• Redis recommended client is Jedis.
• Available at https://github.com/xetorthio/jedis/wiki/Getting-started
Maven dependency
<dependency>
Setting up the Pool <groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
private void setupPool(){ <version>2.0.0</version>
try{ <type>jar</type>
pool = new JedisPool(new JedisPoolConfig(), HOST, PORT, <scope>compile</scope>
poolTimeout) ; </dependency>
jedis = pool.getResource();
System.out.println("Redis Client is connection to: " + HOST + ":" +
PORT) ;
jedis.auth("foobared");
jedis.configSet("timeout", "500");
jedis.connect();
jedis.flushAll();
}catch(Exception ex){
ex.printStackTrace();
}
}
http://redis.io/clients git clone git://github.com/xetorthio/jedis.git
4. Installation
Download, extract and compile Redis with:
$ wget http://redis.googlecode.com/files/redis-2.4.13.tar.gz
$ tar xzf redis-2.4.13.tar.gz
$ cd redis-2.4.13
$ make
6. Basic Usage- Strings
APPEND – Make a string longer
GET – Fetch a string at key
GETRANGE – Fetch a substring of key's value
GETSET – Get and Set in one operation
MGET – Multiple Gets
MSET – Multiple Sets
MSETNX – SET Multiple if Not eXists
SET – SET a value if not exists
SETEX – SET a value that EXpires
SETNX – Set if Not eXists
SETRANGE – Rewrite a substring
STRLEN – Integer length of string
7. Basic Usage- More with Strings
INCR, INCRBY, DECR, DECRBY,
Atomic Counters!
Atomic operation - prevents any
other processor or I/O device from
writing or reading memory until
the operation is complete.
8. More with Strings
In fact, all of the built-in
commands of Redis are atomic.
And they can be chained together
(pipelined) to create complex
atomic transactions.
11. Basic Usage - Lists
LINDEX – Return the list item at a certain position
LINSERT – Insert item into list BEFORE|AFTER item
LLEN – Number of list items (length)
LRANGE – Return some of the items
LREM – Remove one or more items
LSET – Sets the list element at index to value
LTRIM – Lop off some items
LPOP, LPUSH, LPUSHX – Left pop/push
RPOP, RPUSH, RPUSHX, RPOPLPUSH – Right pop/push
BLPOP, BRPOP, BRPOPLPUSH – Blocking pop/push
13. Basic Usage - Sets
SADD – Add a member to a set
SCARD – Set CARDinality – Number of set members
SDIFF – Calculate difference between 2 sets
SDIFFSTORE – Store the Difference of 2 sets in a set
SINTER – Calculate the intersection between 2 sets
SINTERSTORE – Store the intersection of 2 sets in a set
SISMEMBER – Bool test whether X is a member of set S
SMEMBERS – list out all the members
SMOVE – Move a member from a set to another set
SPOP – Pop a member off from a set
SRANDMEMBER – Fetch a random set member
SREM – Remove a member from the set
SUNION – Merge two sets together
SUNIONSTORE – Store the merger of two sets in a set
15. Basic Usage - Hashes
HDEL – Delete a field & value from a hash
HEXISTS – Bool test whether field is in hash
HGET – fetch value stored under hash → field
HGETALL – fetch the whole hash
HINCRBY – Increment a value stored under a hash field
HKEYS – keys as a set → array_keys( )
HLEN – Integer – Number of fields stored in hash
HMGET – Array – Fetch multiple fields from hash
HMSET – Set multiple fields in a hash
HSET – Set one field in a hash
HSETNX – Set a hash field if it doesn't exist
HVALS – vals as a list → array_values( )
17. Basic Usage - Sorted Sets
ZADD – Add a member to a set with a score
ZCARD – Count how many members are in the sorted set
ZCOUNT – Count how many elemenents between 2 scores
ZINCRBY – Increase/Decrease a member's score
ZINTERSTORE – Intersect on scores and store it
ZRANGE – Return some members between 2 indexes
ZRANGEBYSCORE – Return some members between 2 scores
ZRANK – Returns a member index from Lo to Hi
ZREM – Remove a sorted set's member
ZREMRANGEBYRANK – Remove some members by rank
ZREMRANGEBYSCORE – Remove some members by score
ZREVRANGE – Fetch members by index in desc order
ZREVRANGEBYSCORE – Fetch members by score in desc order
ZREVRANK – Returns the index from Hi to Lo
ZSCORE – Fetch the score of a member
ZUNIONSTORE – Merge 2 sorted sets into a 3rd sorted set