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

网站建设淘宝山东城市建设职业学院教务网站

网站建设淘宝,山东城市建设职业学院教务网站,网站建设需要掌握什么知识,免费版crm系统1. 业务需求 前端用户查询数据时,数据查询缓慢耗费时间; 基于缓存中间件实现缓存方法返回值:实现流程用户第一次查询时在数据库查询,并将查询的返回值存储在缓存中间件中,在缓存有效期内前端用户再次查询时,从缓存中间件缓存获取 2. 基于Redis实现 参考1 2.1 简单实现 引入…

1. 业务需求

前端用户查询数据时,数据查询缓慢耗费时间;
基于缓存中间件实现缓存方法返回值:实现流程用户第一次查询时在数据库查询,并将查询的返回值存储在缓存中间件中,在缓存有效期内前端用户再次查询时,从缓存中间件缓存获取

2. 基于Redis实现

参考1

2.1 简单实现

引入cache依赖

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency>

在redis配置类中添加cache缓存机制

/*** redis配置** @author ruoyi*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {/*** 方法返回值缓存策略** @param factory* @return*/@Beanpublic CacheManager cacheManager(RedisConnectionFactory factory) {RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(2 * 60))//过期超时时间 2分钟.disableCachingNullValues();return RedisCacheManager.builder(factory).cacheDefaults(config).transactionAware().build();}//......其他配置不展示.....
}

启动类开始方法缓存@EnableCaching

package com.ruoyi;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;/*** 启动程序** @author ruoyi*/
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@EnableCaching
public class RuoYiApplication
{public static void main(String[] args){// System.setProperty("spring.devtools.restart.enabled", "false");SpringApplication.run(RuoYiApplication.class, args);System.out.println("(♥◠‿◠)ノ゙  若依启动成功   ლ(´ڡ`ლ)゙  \n" +" .-------.       ____     __        \n" +" |  _ _   \\      \\   \\   /  /    \n" +" | ( ' )  |       \\  _. /  '       \n" +" |(_ o _) /        _( )_ .'         \n" +" | (_,_).' __  ___(_ o _)'          \n" +" |  |\\ \\  |  ||   |(_,_)'         \n" +" |  | \\ `'   /|   `-'  /           \n" +" |  |  \\    /  \\      /           \n" +" ''-'   `'-'    `-..-'              ");}
}

业务层设置缓存方法

    @Override@Cacheable(cacheNames = "selectuserlist",key = "#user.userId")public List<SysUser> selectUserList(SysUser user){return userMapper.selectUserList(user);}

测试效果

第一次请求时
在这里插入图片描述
在这里插入图片描述
第二次请求时,没有查询数据库
在这里插入图片描述
查询userId为2的用户
在这里插入图片描述
在这里插入图片描述

两分钟过后
在这里插入图片描述

2.2 设置自定义过期时长

创建RedisCache解析器

package com.ruoyi.framework.config.properties;import com.ruoyi.common.utils.StringUtils;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;import java.time.Duration;/*** RedisCache解析器*/
public class MyRedisCacheManager extends RedisCacheManager {public MyRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {super(cacheWriter, defaultCacheConfiguration);}@Overrideprotected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) {//解析name字段if (!StringUtils.isEmpty(name) && name.contains("#")) {//获取时间String numStr = name.split("#")[1];if (StringUtils.isNumeric(numStr)) {//重置缓存时长return super.createRedisCache(name, cacheConfig.entryTtl(Duration.ofSeconds(Integer.parseInt(numStr))));}}return super.createRedisCache(name, cacheConfig);}
}

配置RedisConfig

package com.ruoyi.framework.config;import com.ruoyi.framework.config.properties.MyRedisCacheManager;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.serializer.StringRedisSerializer;import java.time.Duration;/*** redis配置** @author ruoyi*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {/*** 方法返回值缓存策略** @param factory* @return*/@Beanpublic CacheManager cacheManager(RedisConnectionFactory factory) {RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(2 * 60))//过期超时时间 2分钟.disableCachingNullValues();//        return RedisCacheManager.builder(factory)
//                .cacheDefaults(config)
//                .transactionAware()
//                .build();return new MyRedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(factory), config);}//...............其他配置...略..........
}

使用160秒有效

    @Override@Cacheable(cacheNames = "selectuserlist#160",key = "#user.userId")public List<SysUser> selectUserList(SysUser user){return userMapper.selectUserList(user);}

2.3 注解Cacheable.key

  • 声明访问缓存的键,由于缓存本质上是键值存储,因此每次调用缓存方法时都需要使用键去访问。
  • 缓存数据时使用的 key,可以用它来指定。默认是使用方法参数的值。这个 key 可以使用 spEL 表达式来编写。
  • 这里的EL表达式可以使用方法参数及它们对应的属性。使用方法参数时我们可以直接使用“#参数名”或者“#p参数index”。
@Cacheable(cacheNames="books", key="#isbn")
public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)@Cacheable(cacheNames="books", key="#isbn.rawNumber")
public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)@Cacheable(cacheNames="books", key="#p0")
public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)@Cacheable(cacheNames="books", key="#p0.rawNumber")
public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)
  • spEL表达式
    在这里插入图片描述
http://www.yayakq.cn/news/262275/

相关文章:

  • 关于网站建设的通知张向东
  • 做电影网站需要多大空间网站建设相关制度
  • 建网站有什么要注意的丹阳高铁站对面的规划
  • 浏阳企业网站建设用php做网站的方法
  • 温州做网站哪个好代码网页制作
  • 如何建淘客网站爱原物设计网
  • 国内做网站网站代理烟台企业网站建站模板
  • 网站建设推广优化话术百度怎么推广网站
  • 主流数据网站网络营销企业网站
  • 如何解决网站兼容性问题网站首页图片轮转代码 很好用
  • 做问卷调查的网站有啥怎么自己建设个网站
  • 焦作网站建设哪家好pc站转换手机网站
  • 多语言的网站佛山做网站制作
  • 惠州手机网站建设公众号软文素材
  • 网站中的表单怎么做wordpress页面背景
  • 外贸购物网站模板免费制作
  • 聊城网站建设售后服务怎么黑进网站后台
  • 网站空间可以自己做服务器用网站制作自己app软件
  • 网站运营年度推广方案苏州建设公司
  • 邯郸企业网站建设报价手机网站 设置
  • 网络营销与网站推广的区别网站站欣赏
  • 张家口网站建设哪里好No酒店网站建设
  • 临海做网站的公司便宜网站建设公司
  • 邯山手机网站建设建设网站怎样提要求
  • 哪里可以买域名做网站制作网页的三大技术是哪些
  • 网站链接锚文字怎么做如何做电商创业
  • 陕西省建设厅管理中心网站创意平面设计公司公司排名
  • 之前做的网站推广怎么删除中国建设银行手机登录
  • 网站建设需要报告网站建设就是学淘宝吗
  • 网站开发者模式专门查公司的软件