• (十六)SpringBoot之使用 Caching-


    一、案例

      1.1  引入maven依赖

    <!-- caching -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-cache</artifactId>
            </dependency>
            <dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
            </dependency>

      1.2  配置application.properties

    #echache缓存
    spring.cache.type=ehcache
    spring.cache.ehcache.config=classpath:config/ehcache.xml

      1.3  配置config/ehcache.xml

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="ehcache.xsd">
        <cache name="roncooCache" eternal="false" maxEntriesLocalHeap="0"
            timeToIdleSeconds="50"></cache>
        <!-- eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和 timeToLiveSeconds属性,默认为false -->
        <!-- maxEntriesLocalHeap:堆内存中最大缓存对象数,0没有限制 -->
    
        <!-- timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为 单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了 
            timeToIdleSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清空。 只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以 
            无限期地处于空闲状态 -->
    </ehcache>

      1.4  启用@EnableCaching 注解支持

    @EnableCaching
    @SpringBootApplication
    public class Springboot01Application {
        
        public static void main(String[] args) {
            SpringApplication.run(Springboot01Application.class, args);
        }
    }

      1.5  编写实体类

    • 如下代码,写完后运行Springboot01Application.java会自动在数据库生成表结构
    package com.shyroke.entity;
    
    import java.io.Serializable;
    import java.util.Date;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    
    @Entity
    @JsonIgnoreProperties({ "handler","hibernateLazyInitializer" })  
    @Table(name="user")
    public class UserBean  {
    
        @Id
        @GeneratedValue
        private Integer id;
    
        @Column
        private Date createTime;
    
        @Column
        private String userName;
    
        @Column
        private String userIp;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getUserIp() {
            return userIp;
        }
    
        public void setUserIp(String userIp) {
            this.userIp = userIp;
        }
    
        @Override
        public String toString() {
            return "RoncooUserLog [id=" + id + ", createTime=" + createTime + ", userName=" + userName + ", userIp=" + userIp + "]";
        }
    
    }

      1.6  编写控制器

    package com.shyroke.controller;
    
    import java.util.Date;
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.data.domain.Pageable;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.shyroke.dao.UserMapper;
    import com.shyroke.entity.UserBean;
    import com.shyroke.service.CacheUserService;
    
    @Controller
    @RequestMapping(value = "/")
    public class IndexController {
    
        @Autowired
        private CacheUserService cacheUserService;
    
        
        @RequestMapping(value = "/select")
        @ResponseBody
        public UserBean get() {
            return cacheUserService.getUserById(1);
        }
    
        @RequestMapping(value = "/update")
        @ResponseBody
        public UserBean update() {
            UserBean bean = cacheUserService.getUserById(1);
            bean.setUserName("测试");
            bean.setCreateTime(new Date());
            cacheUserService.update(bean);
            return bean;
        }
    
        @RequestMapping(value = "/del")
        @ResponseBody
        public String del() {
            return cacheUserService.deleteById(1);
        }
    
    }

      1.7  编写服务接口和实现类

    package com.shyroke.service;
    
    import com.shyroke.entity.UserBean;
    
    public interface CacheUserService {
        /**
         * 查询
         * 
         * @param id
         * @return
         */
        public UserBean getUserById(int id);
    
        /**
         * 更新
         * 
         * @param user
         * @return
         */
    
        public UserBean update(UserBean user);
    
        /**
         * 删除
         * 
         * @param id
         * @return
         */
    
        public String deleteById(int id);
    }
    package com.shyroke.service.impl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cache.annotation.CacheConfig;
    import org.springframework.cache.annotation.CacheEvict;
    import org.springframework.cache.annotation.CachePut;
    import org.springframework.cache.annotation.Cacheable;
    import org.springframework.stereotype.Repository;
    import org.springframework.stereotype.Service;
    
    import com.shyroke.dao.UserMapper;
    import com.shyroke.entity.UserBean;
    import com.shyroke.service.CacheUserService;
    
    @CacheConfig(cacheNames = "roncooCache")
    @Service
    public class CacheUserServiceImpl implements CacheUserService{
    
        @Autowired
        private UserMapper userMapper;
        
        @Cacheable(key = "#p0")
        @Override
        public UserBean getUserById(int id) {
            System.out.println("查询功能,缓存找不到,直接读库, id=" + id);
            return userMapper.getOne(id);
        }
    
        @CachePut(key = "#p0")
        @Override
        public UserBean update(UserBean user) {
            System.out.println("更新功能,更新缓存,直接写库, id=" + user);
            return userMapper.save(user);
        }
    
        @CacheEvict(key = "#p0")
        @Override
        public String deleteById(int id) {
            System.out.println("删除功能,删除缓存,直接写库, id=" + id);
             userMapper.delete(id);
             return "删除成功";
        }
    
    }

      1.8  编写userMapper.java

    package com.shyroke.dao;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.stereotype.Repository;
    
    import com.shyroke.entity.UserBean;
    
    public interface UserMapper extends JpaRepository<UserBean, Integer>{
    
    }

      1.9  结果

    • 现在数据库中插入一条数据


    • 第一次访问,所以没有缓存,故发出一条sql语句查询。
    •  然后在访问“http://localhost:8888/select” 结果如下:

    没有发出sql语句,说明是从缓存中读取的数据。



    • 测试更新结果

    • 更新完毕之后,返回更新后的数据,然后在返回“http://localhost:8888/select”

    • 没有发出查询语句,而且查询的结果的更新世间与执行“http://localhost:8888/update”更新操作后的时间一致,说明查询的是最新的更新后的数据,所以表示“http://localhost:8888/update”更新操作会先更新缓存。
  • 相关阅读:
    117. Populating Next Right Pointers in Each Node II
    50. Pow(x, n)
    494. Target Sum
    Word Ladder
    HDU 4417
    POJ 2104
    POJ 3277
    【转】图论500题
    POJ 2991
    POJ 1436
  • 原文地址:https://www.cnblogs.com/shyroke/p/8025604.html
Copyright © 2020-2023  润新知