SSM+Redis瞎搞
之前通过SSM+Jedis勉强入门了,这次搞Redis记录一下关键代码!
初学redis觉定自己搞一个Demo先试试整理一下思路,什么不考虑整,不管对错,试试就试试。
理一下思路,总结一句话:去数据库查数据之前先去redis中读一下,如果没有数据就去数据库查,查到数据后存进redis返回数据。
1、redis.properties写一个方便
########################## ## redis缓存配置 ########################## # redis主机IP redis.host=192.168.229.128 # redis端口 redis.port=6379 # 链接超时 # redis.timeout=2000 # 密码 # redis.password=root # 指定redis数据库 # redis.database=2 ########################## ## redis连接池配置 ########################## # 最大连接数 redis.maxTotal=30 # 最大空闲连接数 redis.maxIdle=10 # 获取链接最大等待毫秒 redis.maxWaitMillis=1000 # 获取链接时检查有效性 redis.testOnBorrow=true
2、肯定得配xml文件(applicationContext-Redis.xml)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- redis 数据源 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 定义最大连接数 --> <property name="maxTotal" value="${redis.maxTotal}" /> <!-- 定义最大空闲链接数 --> <property name="maxIdle" value="${redis.maxIdle}" /> <!-- 定义最长等待时间 --> <property name="maxWaitMillis" value="${redis.maxWaitMillis}" /> <!-- 在获取连接时检查是否有效性 --> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <!-- Spring Data Redis 连接池工厂 --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <!-- redis 主机IP --> <property name="hostName" value="${redis.host}" /> <!-- redis 端口 --> <property name="port" value="${redis.port}" /> <!-- 加载JedisPool配置信息 --> <property name="poolConfig" ref="poolConfig" /> </bean> <!-- 配置RedisTemplate API bean --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <!-- Redis hash key: value 序列化 --> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="connectionFactory" ref="jedisConnectionFactory" /> </bean> </beans>
3、Controller
package com.xxw.controller; import com.xxw.pojo.User; import com.xxw.service.IUserService; import com.xxw.util.LoggerAspect; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; import java.util.List; @Controller @RequestMapping(value = "/user") public class UserController { private final Logger logger = LoggerFactory.getLogger(LoggerAspect.class); @Resource(name = "UserServiceImplement") private IUserService userService; @RequestMapping(value = "getUserName", method = RequestMethod.GET) @ResponseBody public Object getUserName(){ logger.info("查询用户名"); try{ return userService.getUserName(); }catch (Exception e){ e.printStackTrace(); } return null; } }
4、Service接口
package com.xxw.service; import java.util.List; public interface IUserService { List<String> getUserName() throws Exception; }
5、Service实现
package com.xxw.service.impl; import com.xxw.dao.UserDao; import com.xxw.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.util.List; @Service(value = "UserServiceImplement") public class UserServiceImplement implements IUserService { @Autowired RedisTemplate redisTemplate; @Autowired UserDao userDao; @Override public List<String> getUserName() throws Exception { String key = "names"; // redis中存储所有用户名的key List<String> usernames; // 存储所有用户名 // 如果不为空,读取redis中用户名,否则 到数据库取所有用户名存储到redis if(redisTemplate.opsForList().size(key) > 0){ usernames = redisTemplate.opsForList().range(key, 0, -1); }else{ usernames = userDao.findUserNameAll(); redisTemplate.opsForList().leftPushAll(key, usernames); } return usernames; } }
6、(dao和Mapper文件就不贴了,没卵用)直接测试
- 首先记录一下redis
- 第一次运行:http://localhost:8080/jedis/user/getUserName
-
- 控制台
-
- redis
-
- 页面
- 第二次运行:http://localhost:8080/jedis/user/getUserName
- 控制台
-
- redis同上一样
-
- 页面
OK不管怎么说算是成功了。先记录到这