网站建设维护一年费用,直播网站建设模板,学校网站建设方案模板下载,如何建网站教程springboot缓存技术-Ehcache-Redis-memcached 文章目录 springboot缓存技术-Ehcache-Redis-memcachedspring缓存使用方式手机验证码案例缓存供应商变更Ehcache变更缓存供应商Redis缓存供应商变更memcached下载安装memcachedSpringBoot整合memcached spring缓存使用方式
导缓存…springboot缓存技术-Ehcache-Redis-memcached 文章目录 springboot缓存技术-Ehcache-Redis-memcachedspring缓存使用方式手机验证码案例缓存供应商变更Ehcache变更缓存供应商Redis缓存供应商变更memcached下载安装memcachedSpringBoot整合memcached spring缓存使用方式
导缓存技术对应的starter dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId/dependency/dependencies开启缓存
SpringBootApplication
// 开启缓存功能
EnableCaching
public class Springboot19CacheApplication {public static void main(String[] args) {SpringApplication.run(Springboot19CacheApplication.class, args);}}
设置当前操作结果数据进入缓存 OverrideCacheable(value cacheSpace,key #id)public Book getById(Integer id) {return bookDao.selectById(id);} 手机验证码案例 缓存供应商变更Ehcache 修改配置 cache:type: ehcacheehcache:config: classpath:ehcache.xml?xml version1.0 encodingUTF-8?
ehcache xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:noNamespaceSchemaLocationhttp://ehcache.org/ehcache.xsdupdateCheckfalse!--diskStore为缓存路径ehcache分为内存和磁盘两级此属性定义磁盘的缓存位置。参数解释如下user.home – 用户主目录user.dir – 用户当前工作目录java.io.tmpdir – 默认临时文件路径--diskStore pathd:\ehcache/!--defaultCache默认缓存策略当ehcache找不到定义的缓存时则使用这个缓存策略。只能定义一个。--!--name:缓存名称。maxElementsInMemory:缓存最大数目maxElementsOnDisk硬盘最大缓存个数。eternal:对象是否永久有效一但设置了timeout将不起作用。overflowToDisk:是否保存到磁盘当系统当机时timeToIdleSeconds:设置对象在失效前的允许闲置时间单位秒。仅当eternalfalse对象不是永久有效时使用可选属性默认值是0也就是可闲置时间无穷大。timeToLiveSeconds:设置对象在失效前允许存活时间单位秒。最大时间介于创建时间和失效时间之间。仅当eternalfalse对象不是永久有效时使用默认是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这个是大家最熟的先进先出。LFU Less Frequently Used就是上面例子中使用的策略直白一点就是讲一直以来最少被使用的。如上面所讲缓存的元素有一个hit属性hit值最小的将会被清出缓存。LRULeast Recently Used最近最少使用的缓存的元素有一个时间戳当缓存容量满了而又需要腾出地方来缓存新的元素的时候那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。--defaultCacheeternalfalsediskPersistentfalsemaxElementsInMemory1000overflowToDiskfalsetimeToIdleSeconds60timeToLiveSeconds60memoryStoreEvictionPolicyLRU/cachenamesmsCodeeternalfalsediskPersistentfalsemaxElementsInMemory1000overflowToDiskfalsetimeToIdleSeconds60timeToLiveSeconds60memoryStoreEvictionPolicyLRU//ehcache变更缓存供应商Redis
导坐标 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency改配置 cache:type: redisredis:use-key-prefix: truecache-null-values: falsekey-prefix: aatime-to-live: 10sredis:host: localhostport: 6379password: 123456
缓存供应商变更memcached
下载安装memcached SpringBoot整合memcached dependencygroupIdcom.googlecode.xmemcached/groupIdartifactIdxmemcached/artifactIdversion2.4.7/version/dependency因为springboot没有整合所以硬编码要自己配置
memcached:servers: localhost:11211poolSize: 10opTimeout: 3000 Component
Data
ConfigurationProperties(prefix memcached)
public class XMemcachedProperties {private String servers;private int poolSize;private long opTimeout;
} Configuration
public class XMemcachedConfig {Autowiredprivate XMemcachedProperties memcachedProperties;Beanpublic 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中使用xmemcachedAutowiredprivate MemcachedClient memcachedClient;Overridepublic String sendCodeToSMS(String tele) {String code codeUtils.generator(tele);try {memcachedClient.set(tele,0,code);} catch (Exception e) {throw new RuntimeException(e);}return code;}Overridepublic 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);}