LoginSignup
14
15

More than 5 years have passed since last update.

Spring boot キャッシュについて

Posted at

Spring bootでキャッシュを使いたい

ローカルキャッシュさせる方法についていくつかざっくり調べてみました。

Caffeine

依存関係を追加(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) {
        // アカウント名取得
    }

Caffeineの設定

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) {
        // アカウント名取得
    }

キャッシュの有効期限を設定

Factoryの実装する必要がある。

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で検索してヒットした内容なので詳しくは検索必要。
cache nameには自分できめたキャッシュ名を指定、またFactoryも実装したクラスを指定する。

<?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>

14
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
15