关于Spring Boot缓存

我想在Spring Boot中使用缓存。

我大致调查了一下如何使用本地缓存的方法。

咖啡因

添加依赖关系(gradle)

    compile 'org.springframework.boot:spring-boot-starter-cache'
    compile 'com.github.ben-manes.caffeine:caffeine'

定义缓存设置类。

package cachetest.config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;


@Configuration
@EnableCaching
public class CacheConfig {

}

对想要应用缓存的类进行设置

package cachetest.service;

@Service
public class AccountDataService {

    @Cacheable("accountDataCache")    
    public String getName(String accountId) {
        // アカウント名取得
    }

咖啡因的设置 de

在application.yml文件中,我们会记录缓存的配置。
expireAfterAccess表示从最后一次访问开始计算,缓存将被保留指定的秒数。

spring:
  cache:
    cache-names: accountDataCache
    caffeine:
      spec: maximumSize=500, expireAfterAccess=30s

Jcache(Hazelcast)。

添加依赖关系(Gradle)

    compile 'org.springframework.boot:spring-boot-starter-cache'
    compile 'javax.cache:cache-api'
    compile 'com.hazelcast:hazelcast'
    compile 'com.hazelcast:hazelcast-spring'

定义一个缓存配置类

package cachetest.config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;


@Configuration
@EnableCaching
public class CacheConfig {

}

我想对需要缓存的类进行设置。

package cachetest.service;

import javax.cache.annotation.CacheDefaults;
import javax.cache.annotation.CacheResult;
import org.springframework.stereotype.Service;

@Service
@CacheDefaults(cacheName = "accountDataCache")
public class AccountDataService {

    @CacheResult    
    public String getName(String accountId) {
        // アカウント名取得
    }

设置缓存的有效期限

需要实施工厂。

package cachetest.spring.jcache;

import java.util.concurrent.TimeUnit;

import javax.cache.configuration.Factory;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
import javax.cache.expiry.ExpiryPolicy;

public class CacheExpiryFactory implements Factory<ExpiryPolicy> {
    @Override
    public ExpiryPolicy create() {
        return new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 30));
    }
}

部署hazelcast.xml文件。

因为下面的内容是在hazelcast.xml中搜索得到的,所以需要进行详细搜索。
在缓存名称中指定一个自己选择的缓存名称,并指定实现了工厂的类。

<?xml version="1.0" encoding="UTF-8"?>
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.5.xsd"
           xmlns="http://www.hazelcast.com/schema/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <group>
        <name>dev</name>
        <password>dev-pass</password>
    </group>
    <network>
        <port auto-increment="true" port-count="100">5701</port>

        <join>
            <multicast enabled="true">
                <multicast-group>224.2.2.3</multicast-group>
                <multicast-port>54327</multicast-port>
            </multicast>
            <tcp-ip enabled="false"/>
        </join>
    </network>

    <cache name="accountDataCache">
        <key-type class-name="java.lang.Object"/>
        <value-type class-name="java.lang.Object"/>
        <expiry-policy-factory class-name="cachetest.spring.jcache.CacheExpiryFactory "/>
    </cache>
</hazelcast>

bannerAds