Java使用Redis
一、Redis Jedis
1 导入 pom 节点
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
2 连接 Redis
private Jedis jedis;@Before </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> before(){ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">连接jedis</span> jedis=<span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("192.168.201.22"<span style="color: rgba(0, 0, 0, 1)">); jedis.ping(); System.out.println(</span>"连接成功"<span style="color: rgba(0, 0, 0, 1)">); }</span></pre>
3 Jedis 操作 String 类型数据
//jedis 操作 String 类 @Test public void operationString(){ jedis.select(1); //设置一个 String 类型的 key-value jedis.set("setkey01","setvalue01"); //设置 key 有效时间 jedis.expire("k1",10); String setkey01 = jedis.get("setkey01"); System.out.println(setkey01); //修改 数据 jedis.set("setkey01","setvalue02"); //删除数据 jedis.del("setkey01"); //设置多个值 jedis.mset("strkey01","strvalue01","strkey02","strvalue02","strkey03","strvalue03","strkey04","1"); //+1 操作 jedis.incr("strkey04"); //-1 操作 jedis.decr("strkey04"); //拼接 jedis.append("strkey04","append");}
4 jedis 操作 List 集合
//操作 List 集合 @Test public void operationlist(){ jedis.select(1); //设置李 list 值,左侧插入 jedis.lpush("listkey01","张三","李四","王五"); //设置 list 值,右侧插入 jedis.rpush("listkey02","张三","李四","王五"); //获取 list 集合值 List<String> listkey01 = jedis.lrange("listkey01", 0, -1); for (String item:listkey01){System.out.println(item); }</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">更改List集合数据</span> jedis.lset("listkey01", 0, "赵六"<span style="color: rgba(0, 0, 0, 1)">); }</span></pre>
5 jedis 操作无序 set 集合
//操作无序 set 集合 @Test public void operationset(){ jedis.select(1); //设置 set 集合 jedis.sadd("setkey01","张三","李四","王五"); //读取 Set<String> setkey01 =jedis.smembers("setkey01"); for (String item:setkey01){System.out.println(item); } }
6 jedis 操作有序 set 集合
//操作有序 set 集合 @Test public void operationsortedset(){ jedis.select(1); //设置 SortedSet 集合 jedis.zadd("setkey02",1,"北京"); jedis.zadd("setkey02",2,"上海"); //获取 SortedSet Set<String>setkey02 =jedis.zrange("setkey02",0,-1); for (String item:setkey02){System.out.println(item); } }
7 jedis 操作 map 集合
//操作 map 集合 @Test public void operationmap(){ jedis.select(1); //设置 map 值 Map<String ,String> map=new HashMap<>(); map.put("name","张三"); map.put("age","18"); jedis.hmset("mapkey01",map);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取值</span> List<String >value=jedis.hmget("mapkey01","name","age"<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 item:value){ System.out.println(item); } }</span></pre>
二、Java 代码
1 导入 pom 节点
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2 创建配置文件 application.yml
3 创建 SpringBootRedis 类
@Service public class SpringBootRedis { @Resource private StringRedisTemplate stringRedisTemplate;</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> set(String key,Object value){ </span><span style="color: rgba(0, 0, 255, 1)">if</span> (value <span style="color: rgba(0, 0, 255, 1)">instanceof</span><span style="color: rgba(0, 0, 0, 1)"> String){ stringRedisTemplate.opsForValue().set(key,(String)value,</span>200L<span style="color: rgba(0, 0, 0, 1)">, TimeUnit.SECONDS); }</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(value <span style="color: rgba(0, 0, 255, 1)">instanceof</span><span style="color: rgba(0, 0, 0, 1)"> List){ List</span><String> list=<span style="color: rgba(0, 0, 0, 1)">(List)value; </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String item:list){ stringRedisTemplate.opsForList().leftPush(key,item); } }</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(value <span style="color: rgba(0, 0, 255, 1)">instanceof</span><span style="color: rgba(0, 0, 0, 1)"> Set){ String[]objects</span>=(String[])((Set)value).toArray(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> String[((Set)value).size()]); stringRedisTemplate.opsForSet().add(key,objects); }</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(value <span style="color: rgba(0, 0, 255, 1)">instanceof</span><span style="color: rgba(0, 0, 0, 1)"> Map){ stringRedisTemplate.opsForHash().putAll(key,(Map</span><?,?><span style="color: rgba(0, 0, 0, 1)">)value); } }
}
4 创建 Controller 层 RedisController
@RestController public class RedisController { @Resource private SpringBootRedis springBootRedis;</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">操作String类型数据</span> @RequestMapping("/setString"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setString(){ springBootRedis.set(</span>"strkey","strvalue"<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)">操作list集合数据 </span> @RequestMapping("/setList"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setList(){ List</span><String > strList=<span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">(); strList.add(</span>"张三"<span style="color: rgba(0, 0, 0, 1)">); strList.add(</span>"李四"<span style="color: rgba(0, 0, 0, 1)">); strList.add(</span>"王五"<span style="color: rgba(0, 0, 0, 1)">); springBootRedis.set(</span>"listkey"<span style="color: rgba(0, 0, 0, 1)">,strList); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">操作set集合数据</span> @RequestMapping("/setSet"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setSet(){ Set</span><String> set =<span style="color: rgba(0, 0, 255, 1)">new</span> HashSet<><span style="color: rgba(0, 0, 0, 1)">(); set.add(</span>"北京"<span style="color: rgba(0, 0, 0, 1)">); set.add(</span>"上海"<span style="color: rgba(0, 0, 0, 1)">); springBootRedis.set(</span>"setkey"<span style="color: rgba(0, 0, 0, 1)">,set); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">操作map集合数据</span> @RequestMapping("/setMap"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setMap(){ Map map</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> HashMap(); map.put(</span>"name","张三"<span style="color: rgba(0, 0, 0, 1)">); map.put(</span>"age","18"<span style="color: rgba(0, 0, 0, 1)">); springBootRedis.set(</span>"setmap"<span style="color: rgba(0, 0, 0, 1)">,map); }
}