武漢網(wǎng)站制作/建立一個(gè)網(wǎng)站的費(fèi)用
數(shù)據(jù)庫在用戶數(shù)量多,系統(tǒng)訪問量大的時(shí)候,系統(tǒng)性能會(huì)下降,用戶體驗(yàn)差。
1.緩存優(yōu)化
作用:
???????1.降低數(shù)據(jù)庫的訪問壓力
???????2.提高系統(tǒng)的訪問性能
???????3.從而提高用戶體驗(yàn)
實(shí)現(xiàn)思路:
1.先查詢緩存
2.如果緩存有數(shù)據(jù),直接返回
3.如果緩存中沒有數(shù)據(jù),則需要查詢數(shù)據(jù)庫,再將數(shù)據(jù)庫查詢的結(jié)果,緩存到redis中。
4.如果數(shù)據(jù)庫中的數(shù)據(jù)發(fā)生修改,緩存數(shù)據(jù)應(yīng)當(dāng)清空,保證和數(shù)據(jù)庫中的數(shù)據(jù)一致!(下一次查詢會(huì)查詢數(shù)據(jù)庫,然后最新的數(shù)據(jù)就到緩存了)
2.使用Redis緩存優(yōu)化
1.環(huán)境搭建
1.導(dǎo)入maven坐標(biāo)
<!--spring Data Redis--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
2.修改配置文件
# redis相關(guān)配置redis:host: ip地址port: 6379password: 密碼database: 0 # 操作的是0號(hào)數(shù)據(jù)庫jedis:#redis連接池配置pool:max-active: 8 #最大連接數(shù)max-wait: 1ms #連接池最大阻塞等待時(shí)間max-idle: 4 #連接池中最大空閑連接min-idle: 0 #連接池中最小空閑連接
3.導(dǎo)入配置類
/*** Redis配置類*/
@Configuration
public class RedisConfig extends CachingConfigurerSupport {@Beanpublic RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory connectionFactory){RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();//默認(rèn)的key序列化器為:JdkSerializationRedisSerializerredisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setConnectionFactory(connectionFactory);return redisTemplate;}
}
2.使用RedisTemplate操作Redis
在高并發(fā)的情況下,頻繁地查詢數(shù)據(jù)庫會(huì)導(dǎo)致系統(tǒng)性能下降,服務(wù)端響應(yīng)時(shí)間增加,需要對(duì)這些Controller方法進(jìn)行緩存優(yōu)化,提高系統(tǒng)的性能。
實(shí)現(xiàn)思路:
????????1.先查詢r(jià)edis,如果redis中有就直接返回結(jié)果,如果沒有就去查詢數(shù)據(jù)庫,并將查詢到的結(jié)果放入Redis并指定有效期。
????????2.在新增,刪除和修改操作的時(shí)候,清空對(duì)應(yīng)的緩存,保證數(shù)據(jù)庫中數(shù)據(jù)和緩存中數(shù)據(jù)一致。
3.Spring?Cache(重點(diǎn))
Spring Cache是一個(gè)框架,實(shí)現(xiàn)了基于注解的緩存功能,只需要簡(jiǎn)單地加一個(gè)注解,就能實(shí)現(xiàn)緩存功能。
Spring Cache提供了一層抽象,底層可以切換不同的cache實(shí)現(xiàn),具體就是通過CacheManager接口來統(tǒng)一不同的緩存技術(shù)。
????????????ChacheManager是Spring提供的各種緩存技術(shù)抽象接口
????????????EhCacheCacheManager :使用EhCache作為緩存技術(shù)
????????????GuavaCacheManager:使用Google的GuavaCache作為緩存技術(shù)
????????????RedisCacheManager:使用Redis作為緩存技術(shù)
使用步驟:
1.導(dǎo)入Maven坐標(biāo):(使用redis緩存技術(shù))
<!--spring cache--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency>
<!--spring Data Redis--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
2.配置application.yml
spring:cache:redis:time-to-live: 1800000?#設(shè)置緩存有效時(shí)間
3.在啟動(dòng)類上加入@EnableCaching注解,開啟緩存注解功能
@SpringBootApplication
@EnableCaching
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args);}
}
4.在Controller方法上加入@Cacheable,@CacheEvict等注解,進(jìn)行緩存操作
@EnableCaching | 開啟緩存注解功能 |
@Cacheable | 在方法執(zhí)行前spring先查看緩存中是否有數(shù)據(jù),如果有數(shù)據(jù),則直接返回緩存數(shù)據(jù);若沒有數(shù)據(jù),調(diào)用方法并將方法返回值放到緩存中。 |
@CachePut | 將方法的返回值放到緩存中 |
@CacheEvict | 將一條或多條數(shù)據(jù)從緩存中刪除 |
注意:在Spring Boot項(xiàng)目中使用緩存技術(shù)只需在項(xiàng)目中導(dǎo)入相關(guān)緩存技術(shù)的依賴包,并在啟動(dòng)類上使用@EnableCaching開啟緩存支持即可。

例如:使用Redis作為緩存技術(shù),只需要導(dǎo)入Spring data Redis的maven坐標(biāo)即可。
對(duì)象在網(wǎng)絡(luò)中傳輸需要實(shí)現(xiàn)序列化接口。

@Slf4j
@RestController
@RequestMapping("user")
public class UserController {@Autowiredprivate UserService userService;@Autowiredprivate CacheManager cacheManager;/** 緩存測(cè)試 @Cacheable** @Cacheable:* 作用:* 1.可以在方法執(zhí)行前,先自動(dòng)查詢緩存,如果緩存中存在數(shù)據(jù),就直接返回(此方法不再執(zhí)行)* 2.如果緩存中沒有數(shù)據(jù),執(zhí)行此方法,并且將方法返回值自動(dòng)存入redis** 屬性:* cacheNames:key名稱空間* key: 名稱空間下的key* 最終redis存儲(chǔ)的key:[cacheNames::key]* 最終redis存儲(chǔ)的key:[userCache::1]*/@Cacheable(cacheNames = "userCache",key = "#id")@GetMapping("/{id}")public User one(@PathVariable Long id){User user = userService.getById(id);log.info("緩存中沒有查數(shù)據(jù)庫");return user;}/** 測(cè)試緩存清理:@CacheEvict** @CacheEvict* 作用:* 當(dāng)方法執(zhí)行完畢后,去清理對(duì)應(yīng)的緩存數(shù)據(jù)** allEntries = true:代表把當(dāng)前名稱空間下的key都清理(默認(rèn)false)* */
// @CacheEvict(cacheNames = "userCache",allEntries = true)@CacheEvict(cacheNames = "userCache",key = "#id")@DeleteMapping("/{id}")public void delete(@PathVariable Long id){userService.removeById(id);}@CacheEvict(cacheNames = "userCache",key = "#user.id")@PutMappingpublic User update(@RequestBody User user){userService.updateById(user);return user;}
}

cacheNames和key相當(dāng)于這樣的結(jié)構(gòu)