spring boot整合redis
来源:互联网 发布:钢笔字 知乎 编辑:程序博客网 时间:2024/06/02 05:54
代码
RedisConfig.java
package com.test.spring.config;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.*;import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;/** * Created by Administrator on 2017/3/1 14:45. */@Configurationpublic class RedisConfig { /** * 注入 RedisConnectionFactory */ @Autowired RedisConnectionFactory redisConnectionFactory; /** * 实例化 RedisTemplate 对象 * * @return */ @Bean public RedisTemplate<String, Object> functionDomainRedisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); initDomainRedisTemplate(redisTemplate, redisConnectionFactory); return redisTemplate; } /** * 设置数据存入 redis 的序列化方式 * * @param redisTemplate * @param factory */ private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) { redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setConnectionFactory(factory); } /** * 实例化 HashOperations 对象,可以使用 Hash 类型操作 * * @param redisTemplate * @return */ @Bean public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForHash(); } /** * 实例化 ValueOperations 对象,可以使用 String 操作 * * @param redisTemplate * @return */ @Bean public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForValue(); } /** * 实例化 ListOperations 对象,可以使用 List 操作 * * @param redisTemplate * @return */ @Bean public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForList(); } /** * 实例化 SetOperations 对象,可以使用 Set 操作 * * @param redisTemplate * @return */ @Bean public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForSet(); } /** * 实例化 ZSetOperations 对象,可以使用 ZSet 操作 * * @param redisTemplate * @return */ @Bean public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForZSet(); }}
RedisModel.java
package com.test.spring.model;import java.io.Serializable;/** * Created by Administrator on 2017/3/1 14:55. */public class RedisModel implements Serializable { private String redisKey;//redis中的key private String name;//姓名 private String tel;//电话 private String address;//住址 public String getRedisKey() { return redisKey; } public void setRedisKey(String redisKey) { this.redisKey = redisKey; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }}IRedisService.javapackage com.test.spring.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.HashOperations;import org.springframework.data.redis.core.RedisTemplate;import javax.annotation.Resource;import java.util.List;import java.util.Set;import java.util.concurrent.TimeUnit;/** * Created by Administrator on 2017/3/1 14:57. */public abstract class IRedisService<T> { @Autowired protected RedisTemplate<String, Object> redisTemplate; @Resource protected HashOperations<String, String, T> hashOperations; /** * 存入redis中的key * * @return */ protected abstract String getRedisKey(); /** * 添加 * * @param key key * @param doamin 对象 * @param expire 过期时间(单位:秒),传入 -1 时表示不设置过期时间 */ public void put(String key, T doamin, long expire) { hashOperations.put(getRedisKey(), key, doamin); if (expire != -1) { redisTemplate.expire(getRedisKey(), expire, TimeUnit.SECONDS); } } /** * 删除 * * @param key 传入key的名称 */ public void remove(String key) { hashOperations.delete(getRedisKey(), key); } /** * 查询 * * @param key 查询的key * @return */ public T get(String key) { return hashOperations.get(getRedisKey(), key); } /** * 获取当前redis库下所有对象 * * @return */ public List<T> getAll() { return hashOperations.values(getRedisKey()); } /** * 查询查询当前redis库下所有key * * @return */ public Set<String> getKeys() { return hashOperations.keys(getRedisKey()); } /** * 判断key是否存在redis中 * * @param key 传入key的名称 * @return */ public boolean isKeyExists(String key) { return hashOperations.hasKey(getRedisKey(), key); } /** * 查询当前key下缓存数量 * * @return */ public long count() { return hashOperations.size(getRedisKey()); } /** * 清空redis */ public void empty() { Set<String> set = hashOperations.keys(getRedisKey()); set.stream().forEach(key -> hashOperations.delete(getRedisKey(), key)); }}RedisServiceImpl.javapackage com.test.spring.serviceimpl;import com.test.spring.model.RedisModel;import com.test.spring.service.IRedisService;import org.springframework.stereotype.Service;/** * Created by Administrator on 2017/3/1 16:00. */@Servicepublic class RedisServiceImpl extends IRedisService<RedisModel> { private static final String REDIS_KEY = "TEST_REDIS_KEY"; @Override protected String getRedisKey() { return this.REDIS_KEY; }}
MainApplication.javapackage com.test.spring;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;/** * Created by Administrator on 2017/2/28 9:40. */@Configuration@EnableAutoConfiguration@ComponentScanpublic class MainApplication { public static void main(String[] args) { System.getProperties().put("projectName", "springApp"); SpringApplication.run(MainApplication.class, args); }}TestController.javapackage com.test.spring.controller;
import com.test.spring.model.RedisModel;
import com.test.spring.serviceimpl.RedisServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by Administrator on 2017/3/1 14:55.
*/
@Controller
public class TestController {
@Autowired
private RedisServiceImpl service;
//添加
@RequestMapping(value = "/add", method = RequestMethod.GET)
@ResponseBody
public void test() {
System.out.println("start.....");
RedisModel m = new RedisModel();
m.setName("张三");
m.setTel("1111");
m.setAddress("深圳1");
m.setRedisKey("zhangsanKey01");
service.put(m.getRedisKey(), m, -1);
RedisModel m2 = new RedisModel();
m2.setName("张三2");
m2.setTel("2222");
m2.setAddress("深圳2");
m2.setRedisKey("zhangsanKey02");
service.put(m2.getRedisKey(), m2, -1);
RedisModel m3 = new RedisModel();
m3.setName("张三3");
m3.setTel("2222");
m3.setAddress("深圳2");
m3.setRedisKey("zhangsanKey03");
service.put(m3.getRedisKey(), m3, -1);
System.out.println("add success end...");
}
//查询所有对象
@RequestMapping(value = "/getAll", method = RequestMethod.GET)
@ResponseBody
public Object getAll() {
return service.getAll();
}
//查询所有key
@RequestMapping(value = "/getKeys", method = RequestMethod.GET)
@ResponseBody
public Object getKeys() {
return service.getKeys();
}
//根据key查询
@RequestMapping(value = "/get", method = RequestMethod.GET)
@ResponseBody
public Object get() {
RedisModel m = new RedisModel();
m.setRedisKey("zhangsanKey02");
return service.get(m.getRedisKey());
}
//删除
@RequestMapping(value = "/remove", method = RequestMethod.GET)
@ResponseBody
public void remove() {
RedisModel m = new RedisModel();
m.setRedisKey("zhangsanKey01");
service.remove(m.getRedisKey());
}
//判断key是否存在
@RequestMapping(value = "/isKeyExists", method = RequestMethod.GET)
@ResponseBody
public void isKeyExists() {
RedisModel m = new RedisModel();
m.setRedisKey("zhangsanKey01");
boolean flag = service.isKeyExists(m.getRedisKey());
System.out.println("zhangsanKey01 是否存在: "+flag);
}
//查询当前缓存的数量
@RequestMapping(value = "/count", method = RequestMethod.GET)
@ResponseBody
public Object count() {
return service.count();
}
//清空所有key
@RequestMapping(value = "/empty", method = RequestMethod.GET)
@ResponseBody
public void empty() {
service.empty();
}
}
阅读全文
0 0
- spring boot整合redis
- spring boot 整合 redis
- spring boot整合redis
- Spring Boot整合Redis
- spring boot redis整合
- spring-boot整合redis 总结
- (七)Spring Boot 整合Redis
- spring boot shiro redis整合
- spring boot中spring cache 整合redis
- spring boot整合redis主从sentinel
- spring boot 中redis与cookie整合
- spring boot整合redis实现缓存机制
- Spring Boot 整合 Redis 实现缓存操作
- Spring Boot 整合 Redis 实现缓存操作
- Spring boot整合redis小例子
- Spring Boot 整合 Redis 实现缓存操作
- spring boot整合redis(单机+集群)
- Spring Boot 整合 Redis 实现缓存操作
- PHP安装,配置xdebuge,PhpStrom配置,自己写的
- Ubuntu14.04 安装opencv3.0中ippicv_linux_20141027.tgz终端命令窗口在线不能下载
- Unity游戏开发——UnityUGUI——Button动态绑定事件
- NET实现微信公共平台上传下载多媒体文件(转)
- vsftpd+pam+mysql
- spring boot整合redis
- Nodejs+Vuejs实现无限分类关系
- SEO新手必学:充分了解搜索引擎盈利模式
- 51单片机的中断系统(六)
- Use LCD Keyboard Shieid to show color sensor's data which received [1.2]
- Python购物流程结合ATM接口实现
- linux centos7 下 mysql5.6安装
- transient关键字
- JZOJ5372. 【NOIP2017提高A组模拟9.17】猫 链表+贪心+堆优化