Redis应用场景
配置连接Redis
<!--redis客户端 jedis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* Jedis的测试
* Created by Peng 2018/10/27 10:35
*/
public class JedisDemo1 {
@Test
public void demo() {
Jedis jedis = new Jedis("localhost", 6379);
jedis.set("name", "ShuKe");
String value = jedis.get("name");
System.out.println(value);
jedis.close();
}
/**
* 连接池方式连接
*/
@Test
public void demo2() {
//获得连接池的配置对象
JedisPoolConfig config = new JedisPoolConfig();
//设置最大连接数
config.setMaxTotal(30);
//设置最大空闲连接数
config.setMaxIdle(10);
//设置最大等待连接时间 毫秒
config.setMaxWaitMillis(1000);
//获取连接池
JedisPool jedisPool = new JedisPool(config, "localhost", 6379);
//获取核心对象
Jedis jedis = null;
try {
//通过连接池获得对象
jedis = jedisPool.getResource();
jedis.set("age", "11");
System.out.println(jedis.get("age"));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedis != null) {
jedis.close();
}
if (jedisPool != null) {
jedisPool.close();
}
}
}
}
有序集合sorted -set
增删改
127.0.0.1:6379> zadd mysort 70 zs 80 ls 90 ww
(integer) 3
127.0.0.1:6379> zadd mysort 100 zs
(integer) 0
127.0.0.1:6379> zadd mysort 60 tom
(integer) 1
127.0.0.1:6379> zscore mysort zs
"100"
127.0.0.1:6379> zcard mysort
(integer) 4
127.0.0.1:6379> zrem mysort tom ww
(integer) 2
127.0.0.1:6379> zcard mysort
(integer) 2
127.0.0.1:6379>
----------------------------------
范围,排序
127.0.0.1:6379> zadd mysort 80 jack 95 rose
(integer) 2
127.0.0.1:6379> zrange mysort 0 -1
1) "jack"
2) "ls"
3) "rose"
4) "zs"
127.0.0.1:6379> zrange mysort 0 -1 withscores
1) "jack"
2) "80"
3) "ls"
4) "80"
5) "rose"
6) "95"
7) "zs"
8) "100"
127.0.0.1:6379> zrevrange mysort 0 -1 withscores
1) "zs"
2) "100"
3) "rose"
4) "95"
5) "ls"
6) "80"
7) "jack"
8) "80"
127.0.0.1:6379> zremrangebyrank mysort 0 4
(integer) 4
127.0.0.1:6379> zcard mysort
(integer) 0
127.0.0.1:6379> zadd mysort 80 zs 90 ls 100 ws
(integer) 3
127.0.0.1:6379> zremrangebyscore mysort 80 100 //根据分数范围删除
(integer) 3
127.0.0.1:6379> zrange mysort 0 -1
(empty list or set)
127.0.0.1:6379>
127.0.0.1:6379> zadd mysort 70 zs 80 ls 90 ww
(integer) 3
127.0.0.1:6379> zrangebyscore mysort 0 100 //根据分数范围查找
1) "zs"
2) "ls"
3) "ww"
127.0.0.1:6379> zrangebyscore mysort 0 100 limit 0 2
1) "zs"
2) "ls"
127.0.0.1:6379> zincrby mysort 3 ls
"83"
127.0.0.1:6379> zscore mysort ls
"83"
127.0.0.1:6379> zcount mysort 80 90
(integer) 2
1.缓存
存取常用数据结构:
http://www.runoob.com/redis/redis-commands.html
存取对象
使用到
Protostuff
序列化和反序列化
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.12</version>
</dependency>
<!--protostuff的序列化依赖-->
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.0.8</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.0.8</version>
</dependency>
//redis存储Object
@Test
public void testEntiry() {
User user = new User("ShuKe", 12);
User user1 = new User("BeiTa", 22);
Jedis jedis = new Jedis("localhost", 6379);
HashMap map = new HashMap();
map.put("user1", JSON.toJSONString(user));
jedis.hmset("userMap", map);
jedis.lpush("userList", JSON.toJSONString(user));
jedis.lpush("userList", JSON.toJSONString(user1));
jedis.close();
}
//redis读取Object
@Test
public void testGetEntiry() {
Jedis jedis = new Jedis("localhost", 6379);
String redisString = jedis.hget("userMap", "user1");
//method 1
JSONObject object = JSONObject.parseObject(redisString);
String username = (String) object.get("userName");
int age = (int) object.get("age");
System.out.println(username + "," + age);
//method 2
List stringList = jedis.lrange("userList", 0, -1);
for (Object s : stringList) {
System.out.println(s);
}
jedis.close();
}
//存储序列化数据
@Test
public void testserializable() {
Jedis jedis = new Jedis("localhost", 6379);
User user = new User("Tina", 12);
HashMap map = new HashMap();
map.put("map","mapTest");
user.setMap(map);
Phone phone1 = new Phone(1887798879);
Phone phone2 = new Phone(1887798876);
List list= new ArrayList();
list.add(phone1);
list.add(phone2);
user.setPhoneList(list);
try {
RuntimeSchema<User> schema
= RuntimeSchema.createFrom(User.class);
String key = "user1";
byte[] bytes = ProtostuffIOUtil.toByteArray(user, schema,
LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
String result = jedis.set(key.getBytes(), bytes);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis.close();
}
}
//取序列化数据
@Test
public void testGetserializable() {
Jedis jedis = new Jedis("localhost", 6379);
try {
RuntimeSchema<User> schema
= RuntimeSchema.createFrom(User.class);
User user = null;
String key = "user1";
byte[] bytes = jedis.get(key.getBytes());
if (bytes != null) {
//空对象
user = schema.newMessage();
ProtostuffIOUtil.mergeFrom(bytes, user, schema);
//user被反序列化
}
System.out.println(JSON.toJSONString(user));
} catch (Exception e) {
e.printStackTrace();
}finally {
jedis.close();
}
}
2.数据过期处理
//数据过期处理
@Test
public void testTTl() {
Jedis jedis = new Jedis("localhost", 6379);
String key = "keyTime";
jedis.set(key,"I will disapper after 60s!");
//两种方式
//jedis.expire(key,60);//60s后失效
//jedis.setex(key+"1",40,"I will disapper after 40s!");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(jedis.ttl(key));
jedis.persist(key);//想让缓存永不过期
System.out.println(jedis.ttl(key));
System.out.println(jedis.ttl(key+"1"));
jedis.close();
}