美文网首页
数据缓存Cache以及缓存切换--SpringBoot

数据缓存Cache以及缓存切换--SpringBoot

作者: KyGb | 来源:发表于2019-08-24 23:30 被阅读0次

    1.Spring缓存支持
    Spring定义了org.springframework.cache.CacheManager和org.springframework.cache.Cache接口用来统一不同的缓存的技术。其中,CacheManager是Spring提供的各种缓存技术抽象接口


    使用方法:

    @Bean
    public EhCacheCacheManager cacheManager(CacheManager ehCacheCacheManager) {
        return new EhCacheCacheManager(ehCacheCacheManager);
    }
    

    2.声名式缓存注解


    @Cacheable、@CachePut、@CacheEvit都有value属性,指定的是要使用的缓存名称;key属性指定的是
    数据在缓存中的存储的键。

    3.开启声名式缓存支持
    开启声名式缓存支持十分简单,只需在配置类上使用@EnableCaching注解即可

    @Configuration
    @EnableCaching
    public class AppConfig {
    }
    
    
    public interface UserInfoDao extends JpaRepository<UserInfo, Long> {
    }
    
    @Service
    public class UserInfoServiceImpl implements UserInfoService {
        private static Logger log = Logger.getLogger(String.valueOf(UserInfoServiceImpl.class));
        @Autowired
        UserInfoDao userInfoDao;
    
        @Override
        @CachePut(value = "userinfoentityManagerFactorycache",key="#userInfo.id") //@CachePut缓存新增的或更新的数据到缓存,其中缓存名称为people,数据的key是person的id。
        public UserInfo save(UserInfo userInfo) {
            UserInfo u = userInfoDao.save(userInfo);
            log.info("为userinfo的id(也是cache的key):" + u.getId() + "数据做了缓存");
            return u;
        }
    
        @Override
        @CacheEvict(value = "userinfocache") //@CacheEvict从缓存people中删除key为id的数据。
        public void remove(Long id) {
            log.info("删除了userinfo的id(也是cache的key):" + id + "数据缓存");
            userInfoDao.deleteById(id);
        }
    
       @Override
        @Cacheable(value = "userinfocache",key="#userInfo.id") //@Cacheable缓存key为person的id数据到缓存people中。
        public UserInfo findOne(UserInfo userInfo) {
            UserInfo u = userInfoDao.findOne();
            log.info("为userinfo的id(也是cache的key):" + u.getId() + "数据做了缓存");
            return u;
        }
    
    
    
    

    启动类也要加上

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

    4.启动、访问,项目启动后,数据库中会自动生成user_info的表(表明可以自定义)

    1.添加一条数据:


    日志:

    Hibernate: insert into user_info (address, age, name, sex) values (?, ?, ?, ?) 
     c.h.cache.userinfo.UserInfoServiceImpl : 为userinfo的id(也是cache的key):12数据做了缓存
    

    2.再次访问http://127.0.0.1:5000/userinfo/cache?id=12则控制台没有输入。


    3.测试remove
    我们刚才已经为id=12做了缓存,取id=12的值直接从缓存中读取。

    控制台输出:
     c.h.cache.userinfo.UserInfoServiceImpl : 删除了userinfo的id(也是cache的key):12数据缓存 
    Hibernate: select userinfo0_.id as id1_0_0_, userinfo0_.address as address2_0_0_, userinfo0_.age as age3_0_0_, userinfo0_.name as name4_0_0_, userinfo0_.sex as sex5_0_0_ from user_info userinfo0_ where userinfo0_.id=? 
    Hibernate: delete from user_info where id=?
    

    4.再次访问http://127.0.0.1:5000/userinfo/cache?id=12(提前是有值的情况下),会发现控制台重新给做了缓存。

    Hibernate: select userinfo0_.id as id1_0_0_, userinfo0_.address as address2_0_0_, userinfo0_.age as age3_0_0_, userinfo0_.name as name4_0_0_, userinfo0_.sex as sex5_0_0_ from user_info userinfo0_ where userinfo0_.id=? 
    c.h.cache.userinfo.UserInfoServiceImpl : 为userinfo的id(也是cache的key):12数据做了缓存
    

    切换缓存

    1. EchCache

    当我们需要使用EhCache作为缓存技术的时候,只需要在pom.xml中添加EchCache的依赖即可:

    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
    </dependency>
    

    EhCache所需的配置文件ehcache.xml只需放在类路径下,Spring Boot会自动扫描。

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache>
        <cache name="UserInfo" maxElementsInMemory="1000" />
    </ehcache>
    

    Spring Boot会给我们自动配置EhCacheCacheManager的Bean。

    2.Guava

    当我们需要使用Guava作为缓存技术的时候,只需要在pom.xml中添加Guava的依赖即可:

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
    </dependency>
    

    Spring Boot会给我们自动配置GuavaCacheManager的Bean。

    3.Redis

    使用Redis,只需添加下面的依赖即可

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

    Spring Boot将会为我们自动配置RedisCacheManager以及RedisTemplate的Bean。

    相关文章

      网友评论

          本文标题:数据缓存Cache以及缓存切换--SpringBoot

          本文链接:https://www.haomeiwen.com/subject/jtpcectx.html