Java使用Redis实例
在开始在 Java 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 Java redis 驱动,且你的机器上能正常使用 Java。
1)redis 安装步骤参考
redis 安装包与 jedis.jar 提取链接:https://pan.baidu.com/s/1Y2jSlGMsuydbTqJZr_RJqg 提取码:k9pj
一:新建一个 JAVAWEB 项目
点击 finish 即可,web.xml 不重要
再将 jedis.jar 架包放入 WEB-INF 下的 lib 中
二:实践案例
1. 连接本地的 Redis 服务和查看服务是否运行
1)首先打开 redis 服务
2)创建 RedisJava 类
public class RedisJava { public static void main(String[] args) { // 连接本地的 Redis 服务 Jedis jedis = new Jedis("localhost"); System.out.println("连接本地的 Redis 服务成功!"); // 查看服务是否运行 System.out.println("服务 正在运行:" + jedis.ping());} }
运行结果:
需要用到 redis 时我们需要开启 redis 服务
2.Redis Java String(字符串) 实例
public static void main(String[] args) { // 连接本地的 Redis 服务 Jedis jedis = new Jedis("localhost"); System.out.println("连接本地的 Redis 服务成功!"); // 设置 redis 字符串数据 jedis.set("souvc", "http://www.cnblogs.com/liuhongfeng/"); // 获取存储的数据并输出 System.out.println("redis 存储的字符串是:" + jedis.get("souvc"));}
输出结果:
3.Redis Java List(列表) 实例 (1)
public static void main(String[] args) { // 连接本地的 Redis 服务 Jedis jedis = new Jedis("localhost"); System.out.println("连接本地的 Redis 服务成功!");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 存储数据到列表中</span> jedis.lpush("kecheng", "java"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"kecheng", "php"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"kecheng", "Mysql"<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> List<String> list = jedis.lrange("kecheng", 0, 5<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, 255, 1)">int</span> i = 0; i < list.size(); i++<span style="color: rgba(0, 0, 0, 1)">) { System.out.println(</span>"redis list里面存储的值是:" +<span style="color: rgba(0, 0, 0, 1)"> list.get(i)); } }</span></pre>
输出结果:
Redis Java List(列表) 实例 (2)
public static void main(String[] args) { // 连接本地的 Redis 服务 Jedis jedis = new Jedis("localhost"); System.out.println("连接本地的 Redis 服务成功!");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">开始前,先移除所有的内容 </span> jedis.del("java framework"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.lrange(</span>"java framework",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)">先向key java framework中存放三条数据 </span> jedis.lpush("java framework","spring"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"java framework","struts"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"java framework","hibernate"<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)">再取出所有数据jedis.lrange是按范围取出, </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有 </span> System.out.println(jedis.lrange("java framework",0,-1<span style="color: rgba(0, 0, 0, 1)">)); jedis.del(</span>"java framework"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"java framework","spring"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"java framework","struts"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"java framework","hibernate"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.lrange(</span>"java framework",0,-1<span style="color: rgba(0, 0, 0, 1)">)); }</span></pre>
输出结果:
住:若开始前未移除所有内容 jedis.del("java framework"); 结果执行一次将会多一条数据,一下是执行三次之后的结果
4. Redis Java Map 实例
public static void main(String[] args) {Iterator<String> iter=jedis.hkeys("user").iterator(); while (iter.hasNext()){ String key = iter.next(); System.out.println(key+":"+jedis.hmget("user",key));} }</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接本地的 Redis 服务</span> Jedis jedis = <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("localhost"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"连接本地的 Redis 服务成功!"<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> Map<String, String> map = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap<String, String><span style="color: rgba(0, 0, 0, 1)">(); map.put(</span>"name", "xinxin"<span style="color: rgba(0, 0, 0, 1)">); map.put(</span>"age", "22"<span style="color: rgba(0, 0, 0, 1)">); map.put(</span>"qq", "123456"<span style="color: rgba(0, 0, 0, 1)">); jedis.hmset(</span>"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)">取出user中的name,执行结果:[minxr]-->注意结果是一个泛型的List </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第一个参数是存入redis中map对象的key,后面跟的是放入map中的对象的key,后面的key可以跟多个,是可变参数 </span> List<String> rsmap = jedis.hmget("user", "name", "age", "qq"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(rsmap); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除map中的某个键值 </span> jedis.hdel("user","age"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.hmget(</span>"user", "age")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">因为删除了,所以返回的是null </span> System.out.println(jedis.hlen("user")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回key为user的键中存放的值的个数2 </span> System.out.println(jedis.exists("user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">是否存在key为user的记录 返回true </span> System.out.println(jedis.hkeys("user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回map对象中的所有key </span> System.out.println(jedis.hvals("user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回map对象中的所有value </span>
输出结果:
5. Redis Java Set 实例
// 连接本地的 Redis 服务 Jedis jedis = new Jedis("localhost"); System.out.println("连接本地的 Redis 服务成功!");//添加 jedis.sadd("users","liuling"); jedis.sadd("users","xinxin"); jedis.sadd("users","ling"); jedis.sadd("users","zhangxinxin"); jedis.sadd("users","who");}</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">移除noname </span> jedis.srem("users","who"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.smembers(</span>"users"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取所有加入的value </span> System.out.println(jedis.sismember("users", "who"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断 who 是否是user集合的元素 </span> System.out.println(jedis.srandmember("users"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(jedis.scard(</span>"users"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回集合的元素个数 </span>
输出结果:
5. Redis Java Sort 实例
public static void main(String[] args) {</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接本地的 Redis 服务</span> Jedis jedis = <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("localhost"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"连接本地的 Redis 服务成功!"<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)">jedis 排序 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注意,此处的rpush和lpush是List的操作。是一个双向链表(但从表现来看的) </span> jedis.del("a");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">先清除数据,再加入数据进行测试 </span> jedis.rpush("a", "1"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"a","6"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"a","3"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"a","9"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.lrange(</span>"a",0,-1));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> [9, 3, 6, 1] </span> System.out.println(jedis.sort("a")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">[1, 3, 6, 9] </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">输入排序后结果 </span> System.out.println(jedis.lrange("a",0,-1<span style="color: rgba(0, 0, 0, 1)">)); }</span></pre>
输出结果:
注意:如果是出现这个错误,那么是因为设置了密码。
我们需要加上一个 auth 方法进行校验。
// 连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
//jedis.auth("souvc");
System.out.println("连接本地的 Redis 服务成功!");