在aop模式下使用redis 的方式

(1)添加依赖
<dependencyManagement> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.2.RELEASE</version> <scope>import</scope> <type>pom</type>
</dependency> </dependencies> </dependencyManagement> <dependencies>
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> </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-test</artifactId> <scope>test</scope>
</dependency> </dependencies>
(2)配置文件中配置application.yml
spring: redis: host: 192.168.126.129 #写自己的ip port: 6379
(3)创建启动类,如果用注解方式需要在此处添加注解(@EnableCache)
package com.jt; import org.springframework.boot.SpringApplication; import
org.springframework.boot.autoconfigure.SpringBootApplication; //@EnableCache
@SpringBootApplication public class RedisApplication { public static void
main(String[] args) { SpringApplication.run(RedisApplication.class,args); } }
(4)获取菜单基于restemplate(方式一)
package com.jt.service; import com.jt.dao.MenuMapper; import com.jt.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired; import
org.springframework.data.redis.core.ValueOperations; import
org.springframework.stereotype.Service; import javax.annotation.Resource;
import java.time.Duration; @Service public class MenuServiceImpl implements
MenuService{ @Autowired private MenuMapper menuMapper; // @Autowired // private
RedisTemplate redisTemplate; @Resource(name="redisTemplate") private
ValueOperations valueOperations;//从spring.io官方的data项目中去查这种注入方式 /** *
基于id查询菜单信息,要求: * 1)先查redis,redis没有去查mysql * 2)将从mysql查询到的数据存储到redis * @param id
* @return */ @Override public Menu selectById(Long id) { //ValueOperations
valueOperations = redisTemplate.opsForValue(); Object
obj=valueOperations.get(String.valueOf(id)); if(obj!=null){
System.out.println("Get Data from redis"); return (Menu)obj; } Menu
menu=menuMapper.selectById(id); valueOperations.set(String.valueOf(id), menu,
Duration.ofSeconds(120)); return menu; } @Override public Menu insertMenu(Menu
menu) { menuMapper.insert(menu); // ValueOperations valueOperations =
redisTemplate.opsForValue(); valueOperations.set(String.valueOf(menu.getId()),
menu, Duration.ofSeconds(120)); return menu; } @Override public Menu
updateMenu(Menu menu) { menuMapper.updateById(menu); // ValueOperations
valueOperations = redisTemplate.opsForValue();
valueOperations.set(String.valueOf(menu.getId()), menu,
Duration.ofSeconds(120)); return menu; } }
(5)获取菜单基于注解(AOP方式,方式二)
package com.jt.service; import com.jt.dao.MenuMapper; import com.jt.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired; import
org.springframework.cache.annotation.CachePut; import
org.springframework.cache.annotation.Cacheable; import
org.springframework.stereotype.Service; @Service public class
DefaultMenuService implements MenuService{ @Autowired private MenuMapper
menuMapper; /** * 由此注解描述的方法为切入点方法,此方法执行时,底层会通过AOP机制 *
先从缓存取数据,缓存有则直接返回,缓存没有则查数据,最后将查询的数据 * 还会向redis存储一份 * @param id * @return */
@Cacheable(value = "menuCache",key="#id") @Override public Menu selectById(Long
id) { return menuMapper.selectById(id); } /** * CachePut注解的意思是更新缓存 * @param
menu * @return */ @CachePut(value = "menuCache",key="#menu.id") @Override
public Menu insertMenu(Menu menu) { menuMapper.insert(menu); return menu; }
@CachePut(value = "menuCache",key="#menu.id") @Override public Menu
updateMenu(Menu menu) { menuMapper.updateById(menu); return menu; } }
(6)设置redis中的序列化格式
package com.jt; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager; import
org.springframework.cache.annotation.CachingConfigurerSupport; import
org.springframework.context.annotation.Bean; import
org.springframework.context.annotation.Configuration; import
org.springframework.context.annotation.Primary; import
org.springframework.data.redis.cache.RedisCacheConfiguration; import
org.springframework.data.redis.cache.RedisCacheManager; import
org.springframework.data.redis.connection.RedisConnectionFactory; import
org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import
org.springframework.data.redis.serializer.RedisSerializationContext; import
org.springframework.data.redis.serializer.RedisSerializer; /** *
重构CacheManager对象,其目的是改变AOP方式应用redis的序列化和反序列化的方式. */ @Configuration public class
CacheManagerConfig { /** * 重构CacheManager对象 * @return */ @Bean public
CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
//定义RedisCache配置 RedisCacheConfiguration cacheConfig=
RedisCacheConfiguration.defaultCacheConfig() //定义key的序列化方式 .serializeKeysWith(
RedisSerializationContext.
SerializationPair.fromSerializer(RedisSerializer.string())) //定义value的序列化方式
.serializeValuesWith( RedisSerializationContext.SerializationPair
.fromSerializer(RedisSerializer.json())); return
RedisCacheManager.builder(redisConnectionFactory) .cacheDefaults(cacheConfig)
.build();//建造者模式(复杂对象的创建,建议使用这种方式,封装了对象的创建细节) } }

技术
下载桌面版
GitHub
Gitee
SourceForge
百度网盘(提取码:draw)
云服务器优惠
华为云优惠券
腾讯云优惠券
阿里云优惠券
Vultr优惠券
站点信息
问题反馈
邮箱:[email protected]
吐槽一下
QQ群:766591547
关注微信