SSM+Redis瞎搞
阿新 • • 發佈:2018-06-04
schema 配置 PE ace 第一次運行 location stack param 通過
之前通過SSM+Jedis勉強入門了,這次搞Redis記錄一下關鍵代碼!
初學redis覺定自己搞一個Demo先試試整理一下思路,什麽不考慮整,不管對錯,試試就試試。
理一下思路,總結一句話:去數據庫查數據之前先去redis中讀一下,如果沒有數據就去數據庫查,查到數據後存進redis返回數據。
1、redis.properties寫一個方便
########################## ## redis緩存配置 ########################## # redis主機IP redis.host=192.168.229.128 # redis端口View Coderedis.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"View Code> <!-- 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; } }View Code
4、Service接口
package com.xxw.service; import java.util.List; public interface IUserService { List<String> getUserName() throws Exception; }View Code
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不管怎麽說算是成功了。先記錄到這
SSM+Redis瞎搞