準備工作 1.Linux系統 2.安裝redis(也可以安裝docker,然後再docker中裝redis,本文章就直接用Linux安裝redis做演示) redis下載地址:http://download.redis.io/releases/redis-4.0.14.tar.gz 修改redis, ...
準備工作
1.Linux系統
2.安裝redis(也可以安裝docker,然後再docker中裝redis,本文章就直接用Linux安裝redis做演示)
redis下載地址:http://download.redis.io/releases/redis-4.0.14.tar.gz
修改redis,開啟遠程訪問
找到redis中的redis.conf文件並編輯(在安裝路徑中找到)
vim ./redis.conf
1、找到bind 127.0.0.1並註釋掉
預設127.0.0.1只能本地訪問,註釋掉即可ip訪問
2、修改 protected-mode 屬性值為no
註釋掉並把保護模式禁用以後可以IP訪問
3、修改daemonize屬性將no 改為yes
將daemonize設置為yes即啟動後臺運行
4、開放6379埠
/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
預設不對外開放6379
5、啟動redis
redis-server /myconf/redis.conf
redis-server預設在/usr/local/bin路徑下,redis.conf在redis的安裝路徑下
6、測試連接
redis-cli -h 192.168.126.129 -p 6379
redis-cli -h redis伺服器IP -p 6379 -a 密碼(沒有設置redis密碼不要寫空,否則報錯)
Java代碼編寫
目錄結構
項目源碼結構
一個user表
代碼
pom.xml文件(可以根據自己的需要來添加或修改)
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mybatis 與 spring boot 2.x的整合包 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--mysql JDBC驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> </dependencies>
下麵是springboot的配置文件application.yml,配置redis(裡面都有註釋解釋)
server: port: 8081 #資料庫連接 spring: datasource: url: jdbc:mysql://localhost:3306/mytest_springboot_cache?useUnicode=true driver-class-name: com.mysql.jdbc.Driver username: root password: lzh ## Redis 配置 redis: ## Redis資料庫索引(預設為0) database: 0 ## Redis伺服器地址 host: 192.168.126.129 ## Redis伺服器連接埠 port: 6379 ## Redis伺服器連接密碼(預設為空) password: jedis: pool: ## 連接池最大連接數(使用負值表示沒有限制) #spring.redis.pool.max-active=8 max-active: 8 ## 連接池最大阻塞等待時間(使用負值表示沒有限制) #spring.redis.pool.max-wait=-1 max-wait: -1 ## 連接池中的最大空閑連接 #spring.redis.pool.max-idle=8 max-idle: 8 ## 連接池中的最小空閑連接 #spring.redis.pool.min-idle=0 min-idle: 0 ## 連接超時時間(毫秒) timeout: 1200 #將themilef的預設緩存禁用,熱載入生效 thymeleaf: cache: false #mybatis的下劃線轉駝峰配置 configuration: map-underscore-to-camel-case: true #另外一種列印語句的方式 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #列印sql時的語句 logging: level: com: acong: dao: debug file: d:/logs/bsbdj.log
接著是實體類,這個比較簡單就不多說了
package com.lzh.springbootstudytest.bean; import java.io.Serializable; /** * @author lzh * create 2019-09-18-22:32 */ public class User implements Serializable { private static final long serialVersionUID = 1L; private int uid; private String userName; private String passWord; private int salary; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public User(int uid, String userName, String passWord, int salary) { super(); this.uid = uid; this.userName = userName; this.passWord = passWord; this.salary = salary; } public User() { super(); } }
這是controller類,用於暴露介面訪問
package com.lzh.springbootstudytest.controller; import com.lzh.springbootstudytest.bean.User; import com.lzh.springbootstudytest.service.UserService; 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.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author lzh * create 2019-09-18-22:36 */ @RestController public class TestController { @Autowired private UserService userService; @RequestMapping("/queryAll") public List<User> queryAll(){ List<User> lists = userService.queryAll(); return lists; } @RequestMapping("/findUserById") public Map<String, Object> findUserById(@RequestParam int id){ User user = userService.findUserById(id); Map<String, Object> result = new HashMap<>(); result.put("uid", user.getUid()); result.put("uname", user.getUserName()); result.put("pass", user.getPassWord()); result.put("salary", user.getSalary()); return result; } @RequestMapping("/updateUser") public String updateUser(){ User user = new User(); user.setUid(1); user.setUserName("cat"); user.setPassWord("miaomiao"); user.setSalary(4000); int result = userService.updateUser(user); if(result != 0){ return "update user success"; } return "fail"; } @RequestMapping("/deleteUserById") public String deleteUserById(@RequestParam int id){ int result = userService.deleteUserById(id); if(result != 0){ return "delete success"; } return "delete fail"; } }
接著是Mapper持久層Dao,這裡主要用註解寫比較方便,也可以使用mybatis的xml配置文件寫sql語句
package com.lzh.springbootstudytest.mapper; import com.lzh.springbootstudytest.bean.User; import org.apache.ibatis.annotations.*; import java.util.List; /** * @author lzh * create 2019-09-18-22:32 */ @Mapper public interface UserDao { @Select("select * from user") List<User> queryAll(); @Select("select * from user where uid = #{id}") User findUserById(int id); @Update("UPDATE USER SET username = CASE WHEN (#{userName} != NULL) AND (#{userName} != '') THEN #{userName},PASSWORD = CASE WHEN (#{passWord} != NULL) AND (#{passWord} != '') THEN #{passWord},salary = CASE WHEN (#{salary} != 0) THEN #{salary} WHERE uid = #{uid}") int updateUser(@Param("user") User user); @Delete("delete from user where uid = #{id}") int deleteUserById(int id); }
service層,這裡主要是使用redis模板來寫
package com.lzh.springbootstudytest.service; import com.lzh.springbootstudytest.bean.User; import com.lzh.springbootstudytest.mapper.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Service; import java.util.List; import java.util.concurrent.TimeUnit; /** * @author lzh * create 2019-09-18-22:33 */ @Service public class UserService { @Autowired private UserDao userDao; @Autowired private RedisTemplate redisTemplate; public List<User> queryAll() { return userDao.queryAll(); } /** * 獲取用戶策略:先從緩存中獲取用戶,沒有則取數據表中 數據,再將數據寫入緩存 */ public User findUserById(int id) { String key = "user_" + id; ValueOperations<String, User> operations = redisTemplate.opsForValue(); //判斷redis中是否有鍵為key的緩存 boolean hasKey = redisTemplate.hasKey(key); if (hasKey) { User user = operations.get(key); System.out.println("從緩存中獲得數據:"+user.getUserName()); System.out.println("------------------------------------"); return user; } else { User user = userDao.findUserById(id); System.out.println("查詢資料庫獲得數據:"+user.getUserName()); System.out.println("------------------------------------"); // 寫入緩存 operations.set(key, user, 5, TimeUnit.HOURS); return user; } } /** * 更新用戶策略:先更新數據表,成功之後,刪除原來的緩存,再更新緩存 */ public int updateUser(User user) { ValueOperations<String, User> operations = redisTemplate.opsForValue(); int result = userDao.updateUser(user); if (result != 0) { String key = "user_" + user.getUid(); boolean haskey = redisTemplate.hasKey(key); if (haskey) { redisTemplate.delete(key); System.out.println("刪除緩存中的key-----------> " + key); } // 再將更新後的數據加入緩存 User userNew = userDao.findUserById(user.getUid()); if (userNew != null) { operations.set(key, userNew, 3, TimeUnit.HOURS); } } return result; } /** * 刪除用戶策略:刪除數據表中數據,然後刪除緩存 */ public int deleteUserById(int id) { int result = userDao.deleteUserById(id); String key = "user_" + id; if (result != 0) { boolean hasKey = redisTemplate.hasKey(key); if (hasKey) { redisTemplate.delete(key); System.out.println("刪除了緩存中的key:" + key); } } return result; } }
這裡主要是使用RedisTemplate來對遠程redis操作,每次訪問controller暴露的介面,首先判斷redis緩存中是否存在該數據,若不存在就從資料庫中讀取數據,然後保存到redis緩存中,當下次訪問的時候,就直接從緩存中取出來。這樣就不用每次都執行sql語句,能夠提高訪問速度。 但是在保存數據到緩存中,通過設置鍵和值和超時刪除,註意設置超時刪除緩存時間不要太長,否則會給伺服器帶來壓力。
執行spring boot的啟動類,訪問http://localhost:8081/findUserById?id=1
再次訪問http://localhost:8081/findUserById?id=1就是從緩存中獲取保存的數據