# Spring Cache

{% hint style="warning" %}
**This is the manual for older MicroStream versions (Version < 5.0).**

**The new documentation (Version >= 5.0) is located at:**

[https://docs.microstream.one/](https://docs.microstream.one/manual)
{% endhint %}

First of all add the MicroStream Cache dependency:

{% code title="pom.xml" %}

```markup
<repositories>
    <repository>
        <id>microstream-releases</id>
        <url>https://repo.microstream.one/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>one.microstream</groupId>
        <artifactId>cache</artifactId>
        <version>04.01.00-MS-GA</version>
    </dependency>
</dependencies>
```

{% endcode %}

The core caching abstraction provided by Spring comes in the [spring-context](https://search.maven.org/search?q=g:org.springframework%20a:spring-context) module.&#x20;

```markup
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>...</version>
</dependency>
```

If you use Spring Boot, then add the [spring-boot-starter-cache](https://search.maven.org/search?q=g:org.springframework.boot%20a:spring-boot-starter-cache) package to add the caching dependencies:

```markup
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
```

To enable caching, Spring makes good use of annotations, much like enabling any other configuration level feature in the framework.\
The caching feature can be enabled by simply providing a cache setup component.

```java
@SpringBootApplication
@EnableCaching
public class MyApplication
```

```java
@Component
public class CachingSetup implements JCacheManagerCustomizer
{
    @Override
    public void customize(CacheManager cacheManager)
    {
        cacheManager.createCache("my_cache", new MutableConfiguration<>()
                .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(SECONDS, 10000)))
                .setStoreByValue(true)
                .setStatisticsEnabled(true));
    }
}
```

{% hint style="info" %}
More information about the Spring Cache Abstraction:\
<https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#cache>
{% endhint %}
