MicroStream Reference Manual
MicroStream HomeAPI Docs
4.0
4.0
  • Preface
  • System Requirements
  • License
  • Changelog
  • Installation
  • Data-Store
    • Overview
    • Getting Started
    • Root Instances
    • Configuration
      • Properties
      • Storage Files and Directories
      • Using Channels
      • Housekeeping
      • Backup
      • Lock File
    • Storage Targets
      • Local File System
      • SQL Databases
        • Oracle
        • MySQL
        • SQLite
      • Blob Stores
        • Oracle Cloud
        • Oracle NoSQL
        • Coherence
    • Storing Data
      • Convenience Methods and Explicit Storing (Transactions)
      • Lazy and Eager Storing
      • Transient Fields
      • Best Practice
    • Loading Data
      • Lazy Loading
        • Touched Timestamp, Null-Safe Variant
        • Clearing Lazy References
    • Deleting Data
    • Queries
    • Application Life-Cycle
    • Legacy Type Mapping
      • User Interaction
    • Backup Strategies
    • Import / Export
    • Housekeeping
    • Customizing
      • Custom Type Handler
      • Custom Legacy Type Handler
      • Custom Class Loader
      • Custom Storing Behavior
      • Optional Storage Manager Reference in Entities
    • REST Interface
      • Setup
      • REST API
      • Client GUI
    • FAQ
      • Data Model
      • Data Management
      • File Storage
      • Java Features
      • Miscellaneous
    • Addendum
      • Supported Java Features
      • Specialized Type Handlers
      • Examples and Demo Projects
  • Cache
    • Overview
    • Getting Started
    • Configuration
      • Properties
      • Storage
    • Use Cases
      • Hibernate Second Level Cache
      • Spring Cache
  • Basic Concepts
    • Layered Entities
      • Configuration
      • Defining Entities
      • Creating Entities
      • Updating Entities
      • Versioning
      • Logging
      • Multiple Layers
    • Wrapping
      • Configuration
      • Usage
Powered by GitBook
On this page
  • Prerequisites
  • Hello World
Export as PDF
  1. Cache

Getting Started

PreviousOverviewNextConfiguration

Last updated 3 years ago

This is the manual for older MicroStream versions (Version < 5.0).

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

Prerequisites

pom.xml
<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.00.00-MS-GA</version>
    </dependency>
</dependencies>

Hello World

CachingProvider provider     = Caching.getCachingProvider();  
CacheManager    cacheManager = provider.getCacheManager();   
MutableConfiguration<Integer, String> configuration = new MutableConfiguration<>()  
    .setTypes(Integer.class, String.class)   
    .setStoreByValue(false)   
    .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE));  
Cache<Integer, String> cache = cacheManager.createCache("jCache", configuration); 
cache.put(1, "Hello World"); 
String value = cache.get(1); 

1) Get the default CachingProvider implementation from the application’s classpath. This method will work if and only if there is only one JCache implementation available in the classpath. If there are multiple providers then use the fully qualified name Caching.getCachingProvider("one.microstream.cache.CachingProvider") instead.

2) Get the default CacheManager instance using the provider.

3) Create a cache configuration using MutableConfiguration 4) with key type and value type as Integer and String respectively 5) configured to store the cache entries by reference (not by value) 6) and with an expiry time of one minute defined for entries from the moment they are created.

7) Using the cache manager, create a cache named jCache with the configuration created in step 3.

8) Put some data into the cache

9) And retrieve it.

The same can be done using MicroSteam's CacheConfiguration API. This time we use a EmbeddedStorageManager as a backing store for the cache.

EmbeddedStorageManager storageManager = EmbeddedStorage.start();
CachingProvider provider     = (one.microstream.cache.CachingProvider)Caching.getCachingProvider();  
CacheManager    cacheManager = provider.getCacheManager();   
CacheConfiguration<Integer, String> configuration = CacheConfiguration
			.Builder(Integer.class, String.class, "jCache", storageManager)
			.expiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_HOUR))
			.build();  
Cache<Integer, String> cache = cacheManager.createCache("jCache", configuration); 
cache.put(1, "one"); 
String value = cache.get(1); 
https://docs.microstream.one/