Skip to content
  • Products
    • MicroStream for Java
    • MicroStream for Android
    • MicroStream Serialization
  • Community
    • GitHub
    • Discussion
    • Contribute
    • Issues
    • StackOverflow
  • Resources
    • Get Started
    • Docs
    • Examples
    • Videos
    • Blog
    • Release & Update Plan
  • Services
    • Support
    • Training
  • Company
    • About us
    • Contact
    • Imprint
  • Subscribe to our Newsletter
  • User icon Sign in

Cookie

We use cookies to make it easier to use and to further improve our service.

If you have given us your consent, you can revoke it at any time in the data protection declaration.

Use all cookies Manage cookies Reject cookies

Cookie

?
?
?
Use all cookiessave settingsReject cookiesView Privacy Policy

Necessary

Necessary cookies help make a website usable by enabling basic functions such as page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

Name Anbieter Zweck Ablauf Typ
c3kie_removeCookieWindow microstream.one Saves the consent status of the user whether the cookie window should be displayed. 1 Jahr HTML Local Storage
c3kie_googleAnalytics microstream.one Saves the consent status of the user as to whether Google Analytics is allowed to run. 1 Jahr HTML Local Storage
c3kie_googleAnalytics microstream.one Saves the consent status of the user as to whether Google Analytics is allowed to run. 1 Jahr HTML Local Storage
c3kie_tagManager microstream.one Saves the consent status of the user as to whether Google Tag Manager is allowed to run. 1 Jahr HTML Local Storage
c3kie_facebook microstream.one Saves the consent status of the user as to whether Facebook is allowed to run. 1 Jahr HTML Local Storage
c3kie_matomo microstream.one Saves the consent status of the user as to whether Matomo is allowed to run. 1 Jahr HTML Local Storage
c3kie_youtube microstream.one Saves the consent status of the user as to whether YouTube is allowed to run. 1 Jahr HTML Local Storage

Statistics

Statistics cookies help website owners understand how visitors interact with websites by collecting and reporting information anonymously.

Name Anbieter Zweck Ablauf Typ
_dc_gtm_ Google Used by Google Analytics to limit the request rate. 1 Jahr HTTP Cookie
_gid_ Google Registers a unique ID that is used to generate statistical data on how the visitor uses the website. 2 Jahre HTTP Cookie
_gcl_au Google Used to send data to Google Analytics about the device and visitor behavior. Captures the visitor across devices and marketing channels. Session Pixel Tracker
_gat_ Google Used to store a unique user ID. 1 Tag HTTP Cookie
_gat_gtag_UA_ Google Used to store a unique user ID. 1 Tag HTTP Cookie
yt-player-headers-readable YouTube Used to determine the optimal video quality based on the visitor's device and network settings. Persistent HTML Local Storage

Marketing

Marketing cookies are used to track visitors across websites. The intent is to show ads that are relevant and engaging to the individual user, making them more valuable to publishers and third party advertising providers.

Name Anbieter Zweck Ablauf Typ
VISITOR_INFO1_LIVE YouTube Tries to estimate the range of users on pages with built-in YouTube videos. 179 Tage HTTP Cookie
YSC YouTube Registers a unique ID to keep statistics on which videos from YouTube the user has seen. Session HTTP Cookie
yt.innertube::nextId YouTube Registers a unique ID to keep statistics on which videos from YouTube the user has seen. Persistent HTML Local Storage
yt.innertube::requests YouTube Registers a unique ID to keep statistics on which videos from YouTube the user has seen. Persistent HTML Local Storage
ytidb::LAST_RESULT_ENTRY_KEY YouTube Saves the user's video player settings with embedded YouTube video. Persistent HTML Local Storage
yt-remote-cast-available YouTube Saves the user's video player settings with embedded YouTube video. Session HTML Local Storage
yt-remote-cast-installed YouTube Saves the user's video player settings with embedded YouTube video. Session HTML Local Storage
yt-remote-connected-devices YouTube Saves the user's video player settings with embedded YouTube video. Persistent HTML Local Storage
yt-remote-device-id YouTube Saves the user's video player settings with embedded YouTube video. Persistent HTML Local Storage
yt-remote-fast-check-period YouTube Saves the user's video player settings with embedded YouTube video. Session HTML Local Storage
yt-remote-session-app YouTube Saves the user's video player settings with embedded YouTube video. Session HTML Local Storage
yt-remote-session-name YouTube Saves the user's video player settings with embedded YouTube video. Session HTML Local Storage

MicroStream as Spring Cache provider

Jan52023
Blog

With Spring Cache functionality, you can add in a transparent way caching functionality to your Spring application. We the help of some annotations, you can cache the result of some expensive method calls so that the next time the cached values are used instead of exec using the method again. This is very useful in the case the data do not change often or when caring the data improves performance at the cost of some memory usage.

The Spring Cache feature is designed as an abstract layer so that the developer can decide which implementation will be used to provide the actual caching functionality. MicroStream implements the Java EE JCache specification which is also supported by Spring Cache

Benefits

What are the benefits of using MicroStream as a Spring Cache provider? Within JCache, you have the optional functionality to store the cached values to an external resource. MicroStream implements this functionality so that you can configure that the MicroStream functionality is used to serialise the Cache to a data storage.

This means, that when you restart your application, it loads the cache data from the previous run. And thus data is already available on the first cache hit. But besides this performance benefit, you can also make use of the Data Model Evolution functionality of MicroStream.

If your class structure that holds the cached data is changed, you can still load the old data. The automatic mapping will be able to resolve the structure in many cases. If the changes are larger, you can define a mapping yourself so that you can still load the Cached data at application startup, even if the structure isn’t identical.

Setup

Let us review how you can configure your Spring Boot application to make use of MicroStream as a Cache provider.

First of all, you need to add the dependency of Spring itself to your application. This can be done by adding the spring-boot-starter-cache dependency.

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

 

You also need to activate the Caching functionality in Spring by using the @EnableCaching annotation.

@SpringBootApplication
@EnableCaching
public class DemoApplication {

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

}

 

The next step is adding the required MicroStream dependencies.

        <dependency>
            <groupId>one.microstream</groupId>
            <artifactId>microstream-cache</artifactId>
            <version>${microstream.version}</version>
        </dependency>

        <dependency>
            <groupId>one.microstream</groupId>
            <artifactId>microstream-integrations-spring-boot</artifactId>
            <version>${microstream.version}</version>
        </dependency>

 

The MicroStream integration dependency for Spring Boot is optional. You only need it when you want to configure the StorageManager through the Spring configuration options. For more information about the integration options, have a look at this blog.

As a last step, we can define the different caches that we need for our application. We mainly need to define the expiry policy, after which time cache entries are evicted automatically, and specify the StorageManager instance.

@Component
public class CacheSetup implements JCacheManagerCustomizer {


    private final EmbeddedStorageManager storageManager;

    public CacheSetup(EmbeddedStorageManager storageManager) {
        // Retrieve the MicroStream Manager as Spring Bean
        this.storageManager = storageManager;
    }


    @Override
    public void customize(CacheManager cacheManager) {
        // Define the caches and their Expiration time
        defineCache(cacheManager, "countries", Duration.ONE_MINUTE);
    }


    private void defineCache(CacheManager cacheManager, String cacheName, Duration duration) {
        CacheConfiguration<?, ?> configuration = CacheConfiguration
                .Builder(Object.class, Object.class, cacheName, storageManager)
                .expiryPolicyFactory(CreatedExpiryPolicy.factoryOf(duration))
                .build();

        cacheManager.createCache(cacheName, configuration);
    }
}

 

Custom Key generation

Spring Cache provides a default Key generation algorithm for the cases where you use the @Cacheable and @CacheEvict annotations (see further on). It creates instances of the org.springframework.cache.interceptor.SimpleKey class that will be used as a Key value in the cache. But it has a problem concerning MicroStream serialisation.

The hash value, used to compare the key values and select them, is stored in a transient field by the constructor. But these transient fields are not persisted by MicroStream and thus when loaded from the storage, the hash value is lost, and thus keys no longer match.

Luckily, creating a custom key generation algorithm is simple. You can also opt for a custom PersistenceFieldEvaluator within MicroStream as described on this documentation page about transient fields.

public class CustomKeyGenerator implements KeyGenerator {
    @Override
    public Object generate(Object target, Method method, Object... params) {
        // Key should only depend on parameters so that @Cacheable and @CacheEvict annotated methods result in same key
        return "Key" + StringUtils.arrayToDelimitedString(params, "_");
    }
}

 

A custom key algorithm should only be dependent on the parameters of your methods as shown in the above example. You have access to the Method and Class in case you want to implement a very complex algorithm that needs access to annotations.

This generator can be registered through a Spring Configuration bean that implements CachingConfigurer.

@Configuration
public class CacheConfig implements CachingConfigurer {

    public KeyGenerator keyGenerator() {
        return new CustomKeyGenerator();
    }
}

 

Usage

Now, we have all pieces in place to make use of MicroStream as a Spring Cache provider. When we make use of @Cacheable and @CacheEvict, MicroStream is responsible for keeping the cached data in a MicroStream-maintained data storage. It serialises the cache data to disk for examples that it can be used the next time, even after a process restart.

The only limitation is that a StorageManager that is used for this caching functionality can’t be used at the same time to define your object graph and use it as your persistence solution for your application. The Cache functionality defines its own root for this functionality.
But you can define and use multiple Storage Managers in the same application. And with the planned changes for version 8 of the integration code, you can define multiple managers more easily.

The complete code of this example can be found at this project on GitHub.

Conclusion

MicroStream can be used as a Spring Cache provider. It not only implements the caching functionality but can also store the cache to a data storage, just like any other Object Graph that is maintained by MicroStream.

This has the added benefit that cache data from a previous run of your application is still available and thus cache data are already available for the first request. The end-user experience and performance are improved as a result.

 

 

Category: BlogJanuary 5, 2023Leave a comment
Tags: CacheIntegrationSpring Boot

Author: Rudy De Busscher

Post navigation

PreviousPrevious post:MicroStream is now an Eclipse Foundation MemberNextNext post:A peek into upcoming version 8.0

Related Posts

A peek into upcoming version 8.0
January 26, 2023
MicroStream joins the Eclipse Foundation
MicroStream is now an Eclipse Foundation Member
December 21, 2022
Some Data Model considerations for searching and indexing with MicroStream
December 12, 2022
MicroStream with Helidon MP
November 17, 2022
Store State of a Background process with MicroStream
October 31, 2022
Updates to the Spring Boot integration in version 7.1
October 14, 2022

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Post comment

Search
Categories
  • Blog(40)
  • Conferences(13)
  • General(3)
  • News(18)
  • Releases(12)
Related Posts
  • A peek into upcoming version 8.0
    January 26, 2023
  • MicroStream as Spring Cache provider
    January 5, 2023
  • MicroStream joins the Eclipse Foundation
    MicroStream is now an Eclipse Foundation Member
    December 21, 2022
  • Some Data Model considerations for searching and indexing with MicroStream
    December 12, 2022
  • MicroStream with Helidon MP
    November 17, 2022
  • Store State of a Background process with MicroStream
    October 31, 2022
  • Updates to the Spring Boot integration in version 7.1
    October 14, 2022
  • Using Cloud storage platforms with MicroStream
    September 29, 2022
  • MicroStream Best-Practice
    September 29, 2022
  • Release of MicroStream version 7.1
    September 16, 2022
Tags
Android AWS Cache Cloud connector Cluster Communication Conference Data Persistence Discussion event feature framework free fundamentals Germany GitHub Hackathon Heldion How to InfoQ Integration Java Java-native Java 17 JAX JCON JVM Con logging LTS Micronaut MicroProfile Microservices MicroStream MicroStream 2 MicroStream 4 MicroStream 6 New Features online Open Liberty Open Source Oracle release Serialization Spring Boot Training
MicroStream

Store Java Object Graphs natively, relieved of heavy-weight DBMS Dependencies. Create ultra-fast In- Memory Database Applications & Microservices with Pure Java. The Pure Java Paradigm Shift in Database Development.

Upcoming Event
No news available.

Platforms

  • MicroStream for Java
  • MicroStream for Android

Community

  • GitHub
  • Discussion
  • Contribute
  • Issues
  • StackOverflow
  • MeetUp

Resources

  • Get Started
  • Docs
  • Examples
  • Videos
  • Blog
  • Release plan

Services

  • Support
  • Training

Company

  • About us
  • Contact

Stay Connected

  • Twitter
  • LinkedIn
  • YouTube
  • GitHub
  • StackOverflow
  • MeetUp

Get the latest MicroStream news:

Subscribe

© 2023 MicroStream Software. All rights reserved.
  • Imprint
  • Terms of use
  • Privacy
  • Legal notice

Cookie

We use cookies to make it easier to use and to further improve our service.

If you have given us your consent, you can revoke it at any time in the data protection declaration.

Use all cookies Manage cookies Reject cookies

Cookie

?
?
?
Use all cookiessave settingsReject cookiesView Privacy Policy

Necessary

Necessary cookies help make a website usable by enabling basic functions such as page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

Name Anbieter Zweck Ablauf Typ
c3kie_removeCookieWindow microstream.one Saves the consent status of the user whether the cookie window should be displayed. 1 Jahr HTML Local Storage
c3kie_googleAnalytics microstream.one Saves the consent status of the user as to whether Google Analytics is allowed to run. 1 Jahr HTML Local Storage
c3kie_googleAnalytics microstream.one Saves the consent status of the user as to whether Google Analytics is allowed to run. 1 Jahr HTML Local Storage
c3kie_tagManager microstream.one Saves the consent status of the user as to whether Google Tag Manager is allowed to run. 1 Jahr HTML Local Storage
c3kie_facebook microstream.one Saves the consent status of the user as to whether Facebook is allowed to run. 1 Jahr HTML Local Storage
c3kie_matomo microstream.one Saves the consent status of the user as to whether Matomo is allowed to run. 1 Jahr HTML Local Storage
c3kie_youtube microstream.one Saves the consent status of the user as to whether YouTube is allowed to run. 1 Jahr HTML Local Storage

Statistics

Statistics cookies help website owners understand how visitors interact with websites by collecting and reporting information anonymously.

Name Anbieter Zweck Ablauf Typ
_dc_gtm_ Google Used by Google Analytics to limit the request rate. 1 Jahr HTTP Cookie
_gid_ Google Registers a unique ID that is used to generate statistical data on how the visitor uses the website. 2 Jahre HTTP Cookie
_gcl_au Google Used to send data to Google Analytics about the device and visitor behavior. Captures the visitor across devices and marketing channels. Session Pixel Tracker
_gat_ Google Used to store a unique user ID. 1 Tag HTTP Cookie
_gat_gtag_UA_ Google Used to store a unique user ID. 1 Tag HTTP Cookie
yt-player-headers-readable YouTube Used to determine the optimal video quality based on the visitor's device and network settings. Persistent HTML Local Storage

Marketing

Marketing cookies are used to track visitors across websites. The intent is to show ads that are relevant and engaging to the individual user, making them more valuable to publishers and third party advertising providers.

Name Anbieter Zweck Ablauf Typ
VISITOR_INFO1_LIVE YouTube Tries to estimate the range of users on pages with built-in YouTube videos. 179 Tage HTTP Cookie
YSC YouTube Registers a unique ID to keep statistics on which videos from YouTube the user has seen. Session HTTP Cookie
yt.innertube::nextId YouTube Registers a unique ID to keep statistics on which videos from YouTube the user has seen. Persistent HTML Local Storage
yt.innertube::requests YouTube Registers a unique ID to keep statistics on which videos from YouTube the user has seen. Persistent HTML Local Storage
ytidb::LAST_RESULT_ENTRY_KEY YouTube Saves the user's video player settings with embedded YouTube video. Persistent HTML Local Storage
yt-remote-cast-available YouTube Saves the user's video player settings with embedded YouTube video. Session HTML Local Storage
yt-remote-cast-installed YouTube Saves the user's video player settings with embedded YouTube video. Session HTML Local Storage
yt-remote-connected-devices YouTube Saves the user's video player settings with embedded YouTube video. Persistent HTML Local Storage
yt-remote-device-id YouTube Saves the user's video player settings with embedded YouTube video. Persistent HTML Local Storage
yt-remote-fast-check-period YouTube Saves the user's video player settings with embedded YouTube video. Session HTML Local Storage
yt-remote-session-app YouTube Saves the user's video player settings with embedded YouTube video. Session HTML Local Storage
yt-remote-session-name YouTube Saves the user's video player settings with embedded YouTube video. Session HTML Local Storage