java中使用redis缓存数据库操作
开始在 Java 中使用 Redis 前, 首先需要确保已经安装了 redis 服务及 Java redis 驱动,且你的机器上能正常使用 Java。安装配置 Redis:
maven 配置如下
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
使用 Jedis 连接并测试
import org.junit.Test; import redis.clients.jedis.Jedis;import java.util.*;
/**
@author: dx
@date:2019-03-26
*/
public class RedisJava {private static Jedis jedis;
static {
// 连接本地 redis 服务
jedis = new Jedis("localhost");
}// Redis Java String(字符串) 实例
@Test
public void testString(){
// 设置 redis 字符串数据
jedis.set("top", "this is test on redis ...");
jedis.set("name", "lw");
// 获取存储数据并输出
System.out.printf("top 的数据为: %s\n", jedis.get("top"));
System.out.printf("name 的数据为: %s\n", jedis.get("name"));</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 追加字符</span> jedis.append("name", " 这是我的英文名字缩写!"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"name >> " + jedis.get("name"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除键</span> Long result = jedis.del("top"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(result </span>> 0 ? "删除 top 成功!" : "删除 top 失败!"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1表示成功, 0表示失败。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置多个键值对</span> jedis.mset("age", "13", "addr", "上海市"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.mget(</span>"name", "age", "addr"<span style="color: rgba(0, 0, 0, 1)">));
}
// Redis Java List(列表) 实例
@Test
public void testList(){
// 存储数据到 redis 的 list 中
jedis.lpush("c-list", "china");
jedis.lpush("c-list", "Google");
jedis.lpush("c-list", "Taobao");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取数据并遍历输出 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。</span> List<String> list = jedis.lrange("c-list",0, -1<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String clist : list) { System.out.printf(</span>"list in redis name c-list: %s\n"<span style="color: rgba(0, 0, 0, 1)">, clist); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> lpushx key value 将一个值插入到已存在的列表头部</span> jedis.lpushx("c-list", "tongren"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.lrange(</span>"c-list", 0, -1<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">rpush key value1 [value2] 在列表中添加一个或多个值</span> jedis.rpush("c-list", "lw", "dx"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.lrange(</span>"c-list", 0, -1<span style="color: rgba(0, 0, 0, 1)">));
}
// Redis java Set
@Test
public void testSet() {
// 获取数据并输出
Set<String> keys = jedis.keys("*");
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key);
}</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">向集合添加一个或多个成员</span> jedis.sadd("webSite", "阿里巴巴", "腾讯"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"webSite", "网易"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">SCARD key 获取集合的成员数</span> System.out.println(jedis.scard("webSite"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">SMEMBERS key 返回集合中的所有成员 返回类型列表[阿里巴巴, 腾讯, 网易]注意顺序不唯一</span> System.out.println(jedis.smembers("webSite"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">SSCAN key cursor [MATCH pattern] [COUNT count] 迭代集合中的元素</span> System.out.println(jedis.sscan("webSite", 0<span style="color: rgba(0, 0, 0, 1)">));
}
// Redis java Hash
@Test
public void testHash() {
Map<String, String> map = new HashMap<>();
map.put("name", "lw");
map.put("age", "22");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> hmset 将 map 存入redis中</span> jedis.hmset("user"<span style="color: rgba(0, 0, 0, 1)">, map); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> hmget 读取map数据</span> System.out.println(jedis.hmget("user", "name", "age"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> hkeys 读取 map 中的所有 keys</span> System.out.println(jedis.hkeys("user"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> hvals 读取 map 中的所有 value</span> System.out.println(jedis.hvals("user"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> hlen 获取 map 中的有多少个字段</span> System.out.println(jedis.hlen("user"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取所有的键 迭代操作</span> Iterator<String> iterator = jedis.hkeys("user"<span style="color: rgba(0, 0, 0, 1)">).iterator(); </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)">(iterator.hasNext()){ String key </span>=<span style="color: rgba(0, 0, 0, 1)"> iterator.next(); System.out.println(key </span>+ " : " + jedis.hmget("user"<span style="color: rgba(0, 0, 0, 1)">, key)); }
}
//sorted set 有序 Set
@Test
public void testSortSet() {
// Redis 有序集合和集合一样也是 string 类型元素的集合, 且不允许重复的成员。
// 不同的是每个元素都会关联一个 double 类型的分数。redis 正是通过分数来为集合中的成员进行从小到大的排序。
// 有序集合的成员是唯一的, 但分数 (score) 却可以重复。
jedis.zadd("city", 0, "北京");
jedis.zadd("city", 1, "上海");
jedis.zadd("city", 1, "上海 2");
jedis.zadd("city", 2, "杭州");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ZCARD key 获取有序集合的成员数</span> System.out.println( jedis.zcard("city"<span style="color: rgba(0, 0, 0, 1)">) ); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ZREVRANK key member 返回有序集合中指定成员的排名,有序集成员按分数值递减(从大到小)排序 java中的方法是zrevrangeByScore </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2指的是最大的分数</span> Set<String> a = jedis.zrevrangeByScore("city", 2, 0<span style="color: rgba(0, 0, 0, 1)">); System.out.println( a );
}
}