Java 使用Redis缓存工具的图文详细方法

开始在 Java 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 Java redis 驱动,且你的机器上能正常使用 Java。

(1)Java 的安装配置可以参考我们的 Java 开发环境配置

(2)安装了 redis 服务;

 

 请参考:Windows 环境下使用 Redis 缓存工具的图文详细方法

 

或是:

 

  • 首先你需要下载驱动包,下载 jedis.jar,确保下载最新驱动包。
  • 在你的 classpath 中包含该驱动包。

 

一、新建一个 javaweb 项目。

 

1. 新建一个 Jedis 的项目。

 

 

 

 

效果如下:

 

 

 

二、下载 redis 依赖包。

 

http://yunpan.cn/c36syrdrC6MDx  访问密码 0cd6

 

三、实战案例

 

1. 连接本地的 Redis 服务和查看服务是否运行 

 

package com.souvc.redis;

import redis.clients.jedis.Jedis;
/**

  • 类名: RedisJava </br>
  • 包名: com.souvc.redis
  • 描述: 连接本地的 Redis 服务和查看服务是否运行 </br>
  • 开发人员: souvc </br>
  • 创建时间: 2015-12-9 </br>
  • 发布版本:V1.0 </br>
    */
    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 服务成功!
Server is running: PONG

 

  

以后,我们需要用 redis,需要开启这个服务。

 

2.  Redis Java String(字符串) 实例 

 

package com.souvc.redis;

import redis.clients.jedis.Jedis;
/**

  • 类名: RedisJava </br>

  • 包名: com.souvc.redis

  • 描述: Redis Java String(字符串) 实例 </br>

  • 开发人员: souvc </br>

  • 创建时间: 2015-12-9 </br>

  • 发布版本:V1.0 </br>
    */
    public class RedisStringJava {

    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"));
    }
    }

 

效果如下:

连接本地的 Redis 服务成功!
redis 存储的字符串是: http://www.cnblogs.com/liuhongfeng/

 

 

3. Redis Java List(列表) 实例 (1)

 

package com.souvc.redis;

import java.util.List;

import redis.clients.jedis.Jedis;
/**

  • 类名: RedisListJava </br>

  • 包名: com.souvc.redis

  • 描述: Redis Java List(列表) 实例 </br>

  • 开发人员: souvc </br>

  • 创建时间: 2015-12-9 </br>

  • 发布版本:V1.0 </br>
    */
    public class RedisListJava {
    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&lt;String&gt; 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 &lt; 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));
      }
    

    }
    }

 

效果如下:

  

连接本地的 Redis 服务成功!
redis list 里面存储的值是:Mysql
redis list 里面存储的值是:php
redis list 里面存储的值是:java

 

Redis Java List(列表) 实例 (2)

 

package com.souvc.redis;

import redis.clients.jedis.Jedis;

public class RedisListJava2 {

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">     
 * 方法名:main&lt;/br&gt;
 * 详述:Redis Java List(列表) 实例  2 &lt;/br&gt;
 * 开发人员:souvc &lt;/br&gt;
 * 创建时间:2015-12-9  &lt;/br&gt;
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> args 说明返回值含义
 * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> 说明发生此异常的条件
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> 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)">开始前,先移除所有的内容  </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)">));
}

}

 

 

效果如下:

 

连接本地的 Redis 服务成功!
[]
[hibernate, struts, spring]
[spring, struts, hibernate]

 

 

4. Redis Java Map 实例

 

package com.souvc.redis;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import redis.clients.jedis.Jedis;

public class RedisMapJava {

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">     
 * 方法名:main&lt;/br&gt;
 * 详述:redis操作Map &lt;/br&gt;
 * 开发人员:souvc &lt;/br&gt;
 * 创建时间:2015-12-10  &lt;/br&gt;
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> args 说明返回值含义
 * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> 说明发生此异常的条件
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> 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)">-----添加数据----------  </span>
    Map&lt;String, String&gt; map = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap&lt;String, String&gt;<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]--&gt;注意结果是一个泛型的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&lt;String&gt; 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>
Iterator<String> iter=jedis.hkeys("user").iterator(); while (iter.hasNext()){ String key = iter.next(); System.out.println(key+":"+jedis.hmget("user",key));} }

}

 

 

效果:

连接本地的 Redis 服务成功!
[xinxin, 22, 123456]
[null]
2
true
[qq, name]
[123456, xinxin]
qq:[123456]
name:[xinxin]

 

 

 

5. Redis Java Set 实例

 

package com.souvc.redis;

import redis.clients.jedis.Jedis;

public class RedisSetJava {

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">     
 * 方法名:main&lt;/br&gt;
 * 详述:Redis Java Set 实例&lt;/br&gt;
 * 开发人员:souvc &lt;/br&gt;
 * 创建时间:2015-12-10  &lt;/br&gt;
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> args 说明返回值含义
 * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> 说明发生此异常的条件
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> 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)">删除map中的某个键值  
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">jedis.hdel("user");
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加  </span>
    jedis.sadd("user","liuling"<span style="color: rgba(0, 0, 0, 1)">);  
    jedis.sadd(</span>"user","xinxin"<span style="color: rgba(0, 0, 0, 1)">);  
    jedis.sadd(</span>"user","ling"<span style="color: rgba(0, 0, 0, 1)">);  
    jedis.sadd(</span>"user","zhangxinxin"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.sadd(</span>"user","who"<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)">移除noname  </span>
    jedis.srem("user","who"<span style="color: rgba(0, 0, 0, 1)">);  
    System.out.println(jedis.smembers(</span>"user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取所有加入的value  </span>
    System.out.println(jedis.sismember("user", "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("user"<span style="color: rgba(0, 0, 0, 1)">));  
    System.out.println(jedis.scard(</span>"user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回集合的元素个数  </span>
}

}

 

效果如下:

连接本地的 Redis 服务成功!
[xinxin, liuling, ling, zhangxinxin]
false
ling
4

 

 

5. Redis Java Sort 实例

 

package com.souvc.redis;

import redis.clients.jedis.Jedis;

public class RedisSortJava {

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">     
 * 方法名:main&lt;/br&gt;
 * 详述:jedis 排序   &lt;/br&gt;
 * 开发人员:souvc &lt;/br&gt;
 * 创建时间:2015-12-10  &lt;/br&gt;
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> args 说明返回值含义
 * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> 说明发生此异常的条件
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> 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)">));  
}

}

 

 

效果:

连接本地的 Redis 服务成功!
[9, 3, 6, 1]
[1, 3, 6, 9]
[9, 3, 6, 1]

 

 

注意:如果是出现这个错误,那么是因为设置了密码。

 

 

 

我们需要加上一个 auth 方法进行校验。

 

// 连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
//jedis.auth("souvc");
System.out.println("连接本地的 Redis 服务成功!");