springboot 使用thymeleaf 完成页面缓存

三层缓存架构:

分布式redis缓存
本地热点缓存
nginx缓存

springboot 使用thymeleaf 完成页面缓存

页面缓存实现: 对于变化不太频繁的页面,将其利用thymeleaf手动编译后,将编译好的html以string类型在redis中进行缓存。再通过responsebooy以及XXXmapper(value = “/index”,produces = “text/html;charset=utf-8”)指定按照html进行解析
实现:

依赖:redis、thymeleaf
注入:
 @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;
controller:
 @GetMapping(value = "/index",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String index(Model model, HttpServletResponse response, HttpServletRequest request) {

        String indexhtml = redisTemplate.opsForValue().get("indexhtml");
        if (StringUtils.isEmpty(indexhtml)){
            // 先查缓存 redis 有没有
            List<Type> types1 = null;
            types1 = (List<Type>) iCacheService.getCommonCache("types");
            //本地缓存为空
            if (types1==null || types1.isEmpty()){
                String types = redisTemplate.opsForValue().get("types");
                // 如果为空从数据库查询
                if (StringUtils.isEmpty(types)){
                    //如果没有缓存查数据库
                    types1 = typeService.list();
                    if (types1 != null && !types1.isEmpty()){
                        // list 对象转 json
                        String s = JSON.toJSONString(types1);
                        redisTemplate.opsForValue().set("types",s);
                        iCacheService.setCommonCache("types",types1);
                        model.addAttribute("types",types1);
                    }
                }else {
                    // 有缓存
                    types1 = (List<Type>) JSON.parse(types);
                    iCacheService.setCommonCache("types",types1);
                    //查询水果类型
                    model.addAttribute("types",types1);
                }
            }
            //查询商品
            List<Products> all =null;
            String goodsRedis = redisTemplate.opsForValue().get("goods");
            if (StringUtils.isEmpty(goodsRedis)){
                all = productsService.findAll();
                String s = JSON.toJSONString(all);
                redisTemplate.opsForValue().set("goods",s);
            }else {
                all = JSONArray.parseArray(goodsRedis, Products.class);
            }
            model.addAttribute("goods",all);

            //轮播图
            List<Picture2> pictureList = picture2Service.list();
            model.addAttribute("picture",pictureList);

            // 无法导入SpringWebContext的包
            IWebContext context = new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());

            indexhtml = thymeleafViewResolver.getTemplateEngine().process("index",context);


            //3.将手动渲染后的html存入redis缓存
            if(!StringUtils.isEmpty(indexhtml)){
                redisTemplate.opsForValue().set("indexhtml",indexhtml);
                redisTemplate.expire("g00list", Duration.ofSeconds(60));
            }
        }
        return indexhtml;
    }

步骤:

1.查redise有没有相关缓存
2.若没有调用数据库进行查询
查询后给request域赋值时要注意使用后model赋值
3.手动渲染html 柱状model and views模型
IWebContext context = new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap()); indexhtml = thymeleafViewResolver.getTemplateEngine().process("index",context);
4.放进redis中进行缓存。
设置过期时间

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
## springboot整合thymeleaf ### 1. 导入起步依赖 ```xml org.springframework.boot spring-boot-starter-thymeleaf ``` ### 2. 更改引入版本 ```xml 3.0.2.RELEASE 2.1.1 ``` > 1. springboot自带的thymeleaf依赖为2.1.3版本,使用thymeleaf-layout-dialect版本为2以下版本。 > 2. 使用3或3以上的thymeleaf时,需要thymeleaf-layout-dialect的版本为2或以上。 > 3. 锁定thymeleaf版本时不能使用thymeleaf.version标签,会和springboot内部的依赖标签冲突。应当使用springboot-thymeleaf.version标签来锁定版本。 ### 3. 配置文件配置 ```properties spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML spring.thymeleaf.cache=false ``` > spring.thymeleaf.cache为缓存,需要热部署时,需要设置为false ## 语法 ### 1. 替换标签体内容 ```html 显示欢迎 显示欢迎 ``` ### 2. 替换属性 ```html 显示欢迎 ``` ### 3. 在表达式中访问属性域 ```html 访问属性域 访问请求域 方式一 访问请求域 方式二 访问Session域 访Session域 方式一 访问Application域 方式一 ``` ### 4. 解析url地址 ```html 解析URL地址,获取ContextPath的值 @{}是把ContextPath的值附加到指定的地址前 @{}是把ContextPath的值附加到指定的地址前 ``` ### 5. 直接执行表达式 ```html 直接执行表达式 无转义效果 : [[${attrRequestScope}]] 有转义效果 : [(${attrRequestScope})] ``` ### 6. 分支与迭代 #### 1. if 判断 ```html if判断字符串是否为空 <p th

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值