当前位置: 首页 > news >正文

网网站开发设计关键词查询网站

网网站开发设计,关键词查询网站,今晚8时兰州全面解封,dw做网站图片运用一、缓存介绍 在 Spring Boot 中,可以使用 Spring Cache abstraction 来实现缓存功能。Spring Cache abstraction 是 Spring 框架提供的一个抽象层,它对底层缓存实现(如 Redis、Ehcache、Caffeine 等)进行了封装,使得在…

一、缓存介绍

         在 Spring Boot 中,可以使用 Spring Cache abstraction 来实现缓存功能。Spring Cache abstraction 是 Spring 框架提供的一个抽象层,它对底层缓存实现(如 Redis、Ehcache、Caffeine 等)进行了封装,使得在不同的缓存实现之间切换变得更加方便。

        Spring Cache Abstraction 的实现原理主要是通过在运行时动态创建代理对象来实现的。当一个带有缓存注解的方法被调用时,代理对象首先检查指定的缓存中是否已有方法的返回值,如果缓存中有,则直接返回缓存中的值,否则调用原方法获取返回值,并将返回值存入缓存中,再返回给调用者。

        在具体实现上,Spring Cache Abstraction 依赖于 CacheManager 和 Cache 两个接口来实现对缓存的管理和操作。CacheManager 接口提供了获取特定缓存的实例的能力,而 Cache 接口则提供了实际的缓存操作,如 get、put 和 evict 等。

        同时,在 Spring Boot 中,我们可以通过配置来指定使用的缓存类型以及其他相关属性,比如缓存的过期时间、最大缓存数量等。

二、利用redis实现缓存

spring boot的整体的设计思路是约定大于配置,约定俗成,第一步,我们需要引入redis和cache的相关的依赖

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId>
</dependency>

注意:commons-pool2必须引入,不然可能会报java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig错误

第二步,配置spring boot配置文件application.yml

spring:redis:host: 127.0.0.1password:database: 0port: 6379lettuce:pool:max-idle: 8max-active: 8max-wait: 3000msmin-idle: 0cache:# 指定Redis作为缓存实现type: redis# 指定项目中的cacheNamescache-names:- USERSredis:# 缓存过期时间为10分钟,单位为毫秒time-to-live: 600000# 是否允许缓存空数据,当查询到的结果为空时缓存空数据到redis中cache-null-values: true# 为Redis的KEY拼接前缀key-prefix: "BOOT_CACHE:"# 是否拼接KEY前缀use-key-prefix: true# 是否开启缓存统计enable-statistics: false

第三步,配置序列化器

@Configuration
public class RedisConfig extends CachingConfigurerSupport {@Beanpublic RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {// 获取Properties中Redis的配置信息CacheProperties.Redis redisProperties = cacheProperties.getRedis();// 获取RedisCacheConfiguration的默认配置对象RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();// 指定序列化器为GenericJackson2JsonRedisSerializerconfig = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));// 过期时间设置if (redisProperties.getTimeToLive() != null) {config = config.entryTtl(redisProperties.getTimeToLive());}// KEY前缀配置if (redisProperties.getKeyPrefix() != null) {config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());}// 缓存空值配置if (!redisProperties.isCacheNullValues()) {config = config.disableCachingNullValues();}// 是否启用前缀if (!redisProperties.isUseKeyPrefix()) {config = config.disableKeyPrefix();}return config;}
}

第四步,开启缓存-@EnableCaching

@SpringBootApplication
@EnableCaching
public class Application {public static void main(String[] args) throws Exception {SpringApplication springApplication=new SpringApplication(Application.class);springApplication.setBannerMode(Banner.Mode.OFF);springApplication.run(args);}
}

到此,我们利用redis作为spring boot的缓存已经搭建好了,下面我们来做个测试,这里就不使用数据库了,我们使用数据来自己模拟数据库数据查询,模拟数据访问层

@Repository
@Slf4j
public class UserMapper {public final Map<String, SystemUser> map = new HashMap<>();@PostConstructpublic void init(){SystemPermissions permissions1 = new SystemPermissions("1", "query");SystemPermissions permissions2 = new SystemPermissions("2", "add");Set<SystemPermissions> permissionsSet = new HashSet<>();permissionsSet.add(permissions1);permissionsSet.add(permissions2);SystemRole role = new SystemRole("1", "admin", permissionsSet);Set<SystemRole> roleSet = new HashSet<>();roleSet.add(role);SystemUser user = new SystemUser();user.setUserName("test");user.setUserId(UUID.randomUUID().toString());user.setUserPwd("123456");user.setSystemRoles(roleSet);map.put(user.getUserName(), user);Set<SystemPermissions> permissionsSet1 = new HashSet<>();permissionsSet1.add(permissions1);SystemRole role1 = new SystemRole("2", "user", permissionsSet1);Set<SystemRole> roleSet1 = new HashSet<>();roleSet1.add(role1);SystemUser user1 = new SystemUser();user1.setUserName("test1");user1.setUserId(UUID.randomUUID().toString());user1.setUserPwd("123456");user1.setSystemRoles(roleSet1);map.put(user1.getUserName(), user1);}public SystemUser queryUser(String userName){log.error("queryUser_没有走缓存:"+userName);return map.get(userName);}
}

以上类是自己的类,自己实现时可以换成自己的,编写service

public interface UserService {SystemUser getUserByName(String userName);}@Service
public class UserServiceImpl implements UserService{private final UserMapper userMapper;public UserServiceImpl(UserMapper userMapper) {this.userMapper = userMapper;}@Cacheable(cacheNames = "USERS",key = "#userName")@Overridepublic SystemUser getUserByName(String userName) {return userMapper.queryUser(userName);}
}

编写controller

@RestController
@Slf4j
public class UserController {private final UserService userService;public UserController(UserService userService) {this.userService = userService;}@GetMapping("queryUser")public JsonResult getUser(String userName){SystemUser user=userService.getUserByName(userName);return new JsonResult<>("0", "查询成功", user);}
}

测试,可以看到,此时我们的redis中没有数据

第一次,请求没有走缓存,我们再看redis,已经有了数据,第二次请求直接拿了redis缓存中的数据

http://www.ritt.cn/news/18497.html

相关文章:

  • 免费搭建网站的平台上海seo优化公司bwyseo
  • 优化设计官方网站威海百度seo
  • 百度推广包做网站吗广州seo
  • 个人微网站怎么做搜索引擎营销策略有哪些
  • 深圳如何搭建制作网站小熊猫seo博客
  • 网站备案申请佛山网站建设解决方案
  • 天津手机版建站系统哪个好电子商务网站建设多少钱
  • 怎么修改网站上的内容百度搜索排名优化
  • %2enet网站开发app广告联盟
  • 宁阳网站定制谷歌搜索引擎官网
  • 做生产计划类的网站河南网站推广那家好
  • 西安建设网站推广茶叶seo网站推广与优化方案
  • 全屏式网站营销模式100个经典案例
  • 如果做vr参观网站百度搜索排名与点击有关吗
  • 中国建设建行网站网络销售挣钱吗
  • php网站开发的毕业论文摘要app推广项目
  • 网站转化免费建网站最新视频教程
  • 重庆市建设工程安全管理网站网站点击软件排名
  • 学做网站需要java么互联网十大企业
  • 国外网站前台模板网络推广都有哪些平台
  • 移动端网站建设原则网站外包
  • 网站建设公司广州全国十大婚恋网站排名
  • wordpress做导航站seo整站优化报价
  • ppt模板免费下载百度文库seo和sem的联系
  • 网站建设公司做销售前景好不好百度一下就知道了官网楯
  • 长沙企业建网站费用珠海网站设计
  • jsp网站设计seo关键词软件
  • wordpress建站腾讯云网络推广企业
  • marketing 网站设计百度收录刷排名
  • 如何在office做网站东莞seo搜索