說到redis緩存,我想先說下spring的默認緩存。spring對緩存的支持,是有着一套接入第三方緩存的規範的。第三方的緩存,如redis、Memcached等,若是須要在spring中使用,就要實現CacheManage。 使用spring緩存,通常須要用到三個註解:@Cacheable,@CachePut,@CacheEvict。html
@Cacheable,可用於類上或者方法上。表示方法的返回值能夠被緩存。當標記在一個類上時則表示該類全部的方法都是支持緩存的。對於一個支持緩存的方法,Spring會在其被調用後將其返回值緩存起來,以保證下次利用一樣的參數來執行該方法時能夠直接從緩存中獲取結果,而不須要再次執行該方法。spring的緩存值是以鍵值對形式存在的,默認使用方法參數做爲key值,能夠自定義key值生成策略,或自定義key值。@Cacheable能夠指定兩個屬性,value、key。 value屬性是必須指定的,其表示當前方法的返回值是會被緩存在哪一個Cache上的,對應Cache的名稱。 key屬性是用來指定Spring緩存方法的返回結果時對應的key的。該屬性支持SpringEL表達式。當咱們沒有指定該屬性時,Spring將使用默認策略生成key。java
@Cacheable(value = "coms", key = "#id") public Component get(String id) { return crudService.get(id, Component.class); }
上例中,就是指定了get方法的緩存名稱爲coms,key值爲傳入的id值,緩存值爲Component。web
@CachePut,能夠用於類上或者方法上。表示方法的返回值能夠被更新和緩存。@CachePut標註的方法在執行前不會去檢查緩存中是否存在以前執行過的結果,而是每次都會執行該方法,並將執行結果以鍵值對的形式存入指定的緩存中。@Cacheable和@CachePut常常聯合使用,可是前提是指定相同的緩存名稱和緩存key值。以下:redis
@Cacheable(value = "coms", key = "#id") public Component get(String id) { return crudService.get(id, Component.class); } @CachePut(value = "coms", key = "#id") public Component update(String id) { Component component = crudService.get(id, Component.class); component.setCreateTime(new Date()); crudService.update(component); return component; }
調用update方法時,也會同步更新get方法對應的緩存。spring
@CacheEvict,是用來標註在須要清除緩存元素的方法或類上的。當標記在一個類上時表示其中全部的方法的執行都會觸發緩存的清除操做。@CacheEvict能夠指定的重要屬性包括allEntries,allEntries=true則表示整個緩存名所表明的的緩存都被清空。數據庫
@CacheEvict(value = "coms", allEntries = true) public void delete(String id) { crudService.delete(id, Component.class); }
1. 新建springboot項目,加入maven依賴json
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.0.RELEASE</version> </dependency>
2. 新建啓動類,加入@EnableCaching瀏覽器
@SpringBootApplication @EnableCaching public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } }
3. 至此 ,項目已經能夠啓動,使用默認端口8080。一個很是簡單的springboot項目搭好了。接來下編寫緩存測試類。因爲spring-context包已經提供了默認的緩存類,因此不須要額外都緩存依賴。緩存
@RequestMapping @RestController public class TestCacheController { @GetMapping("/get") @Cacheable(value = "user", key = "#id") public User get(String id) { System.out.println("調用方法自己--get"); User user = new User(); user.setId(id); user.setName("第一"); return user; } @GetMapping("/update") @CachePut(value = "user", key = "#id") public User update(String id,String name) { System.out.println("調用方法自己--update"); User user = new User(); user.setId(id); user.setName(name); return user; } @GetMapping("/delete") @CacheEvict(value = "user", key = "#id") public void delete(String id) { System.out.println("調用方法自己--delete"); } }
public class User implements Serializable{ private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
4.使用瀏覽器,輸入請求訪問屢次測試結果,對比控制檯的輸出,驗證每一個註解是否正常發揮做用。我這邊徹底已經正常了,就不貼出截圖了。springboot
一直都以爲redis是多麼高大上的東西,畢竟歷來沒用過。
Redis是一個高性能的key-value型數據庫,同時還提供list,set,zset,hash等數據結構的存儲,支持數據的持久化。Redis的全部操做都是原子性的。具體的安裝、命令教程均可以從官網查看:http://www.redis.net.cn/tutorial/3501.html。
總結來講,就是一個redis分服務端和客戶端兩部分,服務端負責存儲數據,客戶端鏈接服務端以後,以命令的形式控制服務端。redis一般被用做系統和數據庫之間的臨時內存,系統從redis中獲取數據比從數據庫中直接獲取數據會快不少。
redis集成到spring中,須要實現CacheManager。並導入相關的依賴和配置。
1. 使用上文搭建好的springboot做爲基礎,集成redis。
2.加入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.1.0.RELEASE</version> </dependency>
3. 加入application.properties的相關配置
# Redis數據庫索引(默認爲0) spring.redis.database=0 # Redis服務器地址 spring.redis.host=192.168.65.45 # Redis服務器鏈接端口 spring.redis.port=6379 # Redis服務器鏈接密碼(默認爲空) spring.redis.password=test # 鏈接池最大鏈接數(使用負值表示沒有限制) spring.redis.jedis.pool.max-active=8 # 鏈接池最大阻塞等待時間(使用負值表示沒有限制) spring.redis.jedis.pool.max-wait=-1 # 鏈接池中的最大空閒鏈接 spring.redis.jedis.pool.max-idle=8 # 鏈接池中的最小空閒鏈接 spring.redis.jedis.pool.min-idle=2 # 鏈接超時時間(毫秒) spring.redis.timeout=3000
4. 加入配置類
@Configuration public class RedisConfig extends CachingConfigurerSupport { @Bean("userTemplate") public RedisTemplate<String, User> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, User> userRedisTemplate = new RedisTemplate<String, User>(); Jackson2JsonRedisSerializer<User> userJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<User>(User.class); userRedisTemplate.setValueSerializer(userJackson2JsonRedisSerializer); userRedisTemplate.setHashValueSerializer(userJackson2JsonRedisSerializer); userRedisTemplate.setKeySerializer(new StringRedisSerializer()); userRedisTemplate.setHashKeySerializer(new StringRedisSerializer()); userRedisTemplate.setConnectionFactory(redisConnectionFactory); return userRedisTemplate; } }
5.編寫測試類
@RequestMapping("/redis") @RestController public class TestRedisController { @Autowired @Qualifier("userTemplate") private RedisTemplate<String,User> redisTemplate; @GetMapping("/get") public User get(String id) { if (redisTemplate.opsForValue().get("user")!=null){ return redisTemplate.opsForValue().get("user"); } System.out.println("調用方法自己--get"); User user = new User(); user.setId(id); user.setName("第一"); redisTemplate.opsForValue().set("user",user); return user; } }
瀏覽器訪問,第一次輸出「調用方法自己--get」,可是第二次沒有輸出在,證實緩存可用。而且,經過redis-cli鏈接到redis服務器,輸入「get user」 ,就會發現確實存在着這麼一個緩存: