springboot缓存技术-Ehcache-Redismemcached

spring缓存使用方式

  1. 缓存技术对应starter
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
    </dependencies>
  1. 开启缓存
@SpringBootApplication
// 开启缓存功能
@EnableCaching
public class Springboot19CacheApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot19CacheApplication.class, args);
    }

}

  1. 设置当前操作结果数据进入缓存
  @Override
    @Cacheable(value = "cacheSpace",key = "#id")
    public Book getById(Integer id) {
        return bookDao.selectById(id);
    }

在这里插入图片描述

在这里插入图片描述

手机验证码案例

在这里插入图片描述

缓存供应商变更Ehcache

在这里插入图片描述
修改配置

  cache:
    type: ehcache
    ehcache:
      config: classpath:ehcache.xml

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
         updateCheck="false">
    <!--
       diskStore:为缓存路径ehcache分为内存磁盘两级,此属性定义磁盘缓存位置参数解释如下user.home用户主目录
       user.dir  – 用户当前工作目录
       java.io.tmpdir – 默认临时文件路径
     -->
    <diskStore path="d:ehcache"/>
    <!--
       defaultCache默认缓存策略,当ehcache找不到定义的缓存时,则使用这个缓存策略。只能定义一个-->
    <!--
      name:缓存名称maxElementsInMemory:缓存最大数maxElementsOnDisk硬盘最大缓存个数。
      eternal:对象是否永久有效,一但设置了,timeout将不起作用overflowToDisk:是否保存磁盘,当系统当机时
      timeToIdleSeconds:设置对象失效前的允许闲置时间单位:秒)。仅当eternal=false对象不是永久有效使用,可选属性默认值0,也就是可闲置时间无穷大。
      timeToLiveSeconds:设置对象失效前允许存活时间单位:秒)。最大时间介于创建时间失效时间之间。仅当eternal=false对象不是永久有效使用默认0.,也就是对象存活时间无穷大。
      diskPersistent:是否缓存虚拟机重启数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
      diskSpoolBufferSizeMB:这个参数设置DiskStore磁盘缓存)的缓存区大小默认30MB每个Cache应该自己一个缓冲区。
      diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔默认120秒。
      memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定策略清理内存默认策略LRU最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
      clearOnFlush内存数量最大是否清除memoryStoreEvictionPolicy:可选策略有:LRU最近最少使用,默认策略)、FIFO(先进先出)、LFU最少访问次数)。
      FIFOfirst in first out,这个是大家最熟的,先进先出。
      LFULess Frequently Used就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素一个hit属性hit值最小的将会被清出缓存。
      LRULeast Recently Used最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
   -->
    <defaultCache
            eternal="false"
            diskPersistent="false"
            maxElementsInMemory="1000"
            overflowToDisk="false"
            timeToIdleSeconds="60"
            timeToLiveSeconds="60"
            memoryStoreEvictionPolicy="LRU"/>

    <cache
            name="smsCode"
            eternal="false"
            diskPersistent="false"
            maxElementsInMemory="1000"
            overflowToDisk="false"
            timeToIdleSeconds="60"
            timeToLiveSeconds="60"
            memoryStoreEvictionPolicy="LRU"/>

</ehcache>

变更缓存供应商Redis

坐标

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

配置

  cache:
    type: redis
    redis:
      use-key-prefix: true
      cache-null-values: false
      key-prefix: aa
      time-to-live: 10s
  redis:
    host: localhost
    port: 6379
    password: 123456

缓存供应商变更memcached

下载安装memcached

在这里插入图片描述
在这里插入图片描述

SpringBoot整合memcached

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

        <dependency>
            <groupId>com.googlecode.xmemcached</groupId>
            <artifactId>xmemcached</artifactId>
            <version>2.4.7</version>
        </dependency>

因为springboot没有整合,所以硬编码,要自己配置
在这里插入图片描述

memcached:
  servers: localhost:11211
  poolSize: 10
  opTimeout: 3000

在这里插入图片描述

@Component
@Data
@ConfigurationProperties(prefix = "memcached")
public class XMemcachedProperties {
    private String servers;
    private int poolSize;
    private long opTimeout;
}

在这里插入图片描述

@Configuration
public class XMemcachedConfig {

    @Autowired
    private XMemcachedProperties memcachedProperties;

    @Bean
    public MemcachedClient getMemcachedClient() throws IOException {
        MemcachedClientBuilder memcachedClientBuilder = new XMemcachedClientBuilder(memcachedProperties.getServers());
        memcachedClientBuilder.setConnectionPoolSize(memcachedProperties.getPoolSize());
        memcachedClientBuilder.setOpTimeout(memcachedProperties.getOpTimeout());

        MemcachedClient memcachedClient = memcachedClientBuilder.build();
        return memcachedClient;

    }
}

在这里插入图片描述
在这里插入图片描述


// 以下是springboot中使用xmemcached

    @Autowired
    private MemcachedClient memcachedClient;
    @Override
    public String sendCodeToSMS(String tele) {
        String code = codeUtils.generator(tele);
        try {
            memcachedClient.set(tele,0,code);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return code;
    }

    @Override
    public Boolean checkCode(SMSCode smsCode) {
        String code = "";
        try {
            code = memcachedClient.get(smsCode.getTele()).toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return smsCode.getCode().equals(code);
    }

原文地址:https://blog.csdn.net/shall_zhao/article/details/134743175

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_43904.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注