auto-refresh-cache

Cached object that can be refreshed automatically when cache is expired

License

License

Categories

Categories

Net Auto Application Layer Libs Code Generators
GroupId

GroupId

net.moznion
ArtifactId

ArtifactId

auto-refresh-cache
Last Version

Last Version

0.4.0
Release Date

Release Date

Type

Type

jar
Description

Description

auto-refresh-cache
Cached object that can be refreshed automatically when cache is expired
Project URL

Project URL

https://github.com/moznion/auto-refresh-cache
Source Code Management

Source Code Management

https://github.com/moznion/auto-refresh-cache

Download auto-refresh-cache

How to add to project

<!-- https://jarcasting.com/artifacts/net.moznion/auto-refresh-cache/ -->
<dependency>
    <groupId>net.moznion</groupId>
    <artifactId>auto-refresh-cache</artifactId>
    <version>0.4.0</version>
</dependency>
// https://jarcasting.com/artifacts/net.moznion/auto-refresh-cache/
implementation 'net.moznion:auto-refresh-cache:0.4.0'
// https://jarcasting.com/artifacts/net.moznion/auto-refresh-cache/
implementation ("net.moznion:auto-refresh-cache:0.4.0")
'net.moznion:auto-refresh-cache:jar:0.4.0'
<dependency org="net.moznion" name="auto-refresh-cache" rev="0.4.0">
  <artifact name="auto-refresh-cache" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.moznion', module='auto-refresh-cache', version='0.4.0')
)
libraryDependencies += "net.moznion" % "auto-refresh-cache" % "0.4.0"
[net.moznion/auto-refresh-cache "0.4.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.21

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.10

test (4)

Group / Artifact Type Version
junit : junit jar 4.11
org.assertj : assertj-core jar 3.5.1
com.google.code.findbugs : findbugs jar 3.0.0
org.slf4j : slf4j-simple jar 1.7.21

Project Modules

There are no modules declared in this project.

thin-cache Build Status javadoc.io

Cached object that can be refreshed automatically when cache is expired.

Synopsis

Make a cache instance with no initial value. In this case, initializing of a cache value is postpone until the first call of method of getting.

final ThinCache<Long> thinCache = new ThinCache<>(10, false, new Supplier<Long>() {
    private long i = 0;

    @Override
    public Long get() {
        return ++i;
    }
});

thinCache.get(); // => 1L (initialize cache)
thinCache.get(); // => 1L (hit cache)

// 10 seconds spent...

thinCache.get(); // => 2L (expired, refresh cache)

// Get refreshed value even if it dosen't spend 10 seconds
thinCache.forceGet(); // => 3L (force refresh)

Or you can make a cache instance with initial cache value;

final ThinCache<Long> thinCache = new ThinCache<>(0L, 10, false, new Supplier<Long>() {
    private long i = 0;

    @Override
    public Long get() {
        return ++i;
    }
});

thinCache.get(); // => 0L (hit cache, initial value)
thinCache.get(); // => 0L (hit cache)

// 10 seconds spent...

thinCache.get(); // => 1L (expired, refresh cache)

// Get refreshed value even if it dosen't spend 10 seconds
thinCache.forceGet(); // => 2L (force refresh)

Description

Overview

ThinCache is a cached object that can be refreshed automatically when cache is expired.

It holds the same value until cache is expired, and it refreshes the value by given supplier automatically when cache is expired.

Exceptional Handling

You can control exceptional handling through the constructor argument.

If this boolean value is true, it returns already cached value and suppresses exception when supplier (generator of cache value) raises some exception. Otherwise, it throws exception as it is.

Example:

final ThinCache<Long> notSuppressExceptionCache = new ThinCache<>(10, false, new Supplier<Long>() {
    @Override
    public Long get() {
        // do something
        throw new RuntimeException("Exception!");
    }
});
notSuppressExceptionCache.get(); // throws exception

final ThinCache<Long> suppressExceptionCache = new ThinCache<>(10, true, new Supplier<Long>() {
    @Override
    public Long get() {
        // do something
        throw new RuntimeException("Exception!");
    }
});
suppressExceptionCache.get(); // returns old (previous) cache and suppresses exception

Exception case

If cache value hasn't been initialized, it throws exception as it even if the second argument of constructor is true.

Asynchronously support

This library has two asynchronously methods;

  • ThinCache#getWithRefreshAheadAsync()
  • ThinCache#forceGetWithRefreshAheadAsync()

These methods delegate and schedules a task to refresh cache to the other thread.
They returns always already cached value; refreshed cache value will be available from the next calling.

ThinCache#getWithRefreshAheadAsync()

This method retrieves always already cached value. And schedules a task to refresh cache when cache is expired.

Example:

final ThinCache<Long> longThinCache = new ThinCache<>(10, false, new Supplier<Long>() {
    private long i = 0;

    @Override
    public Long get() {
        return ++i;
    }
});

CacheWithScheduledFuture<Long> cacheWithScheduledFuture = longThinCache.getWithRefreshAheadAsync(); // Cache value hasn't been initialized, so initialize cache *synchronously*
cacheWithScheduledFuture.getCached(); // => 1L

cacheWithScheduledFuture = longThinCache.getWithRefreshAheadAsync(); // Hit cache, it doesn't schedule a task to refresh
cacheWithScheduledFuture.getCached(); // => 1L

// 10 seconds spent...

cacheWithScheduledFuture = longThinCache.getWithRefreshAheadAsync(); // Cache expired but fetch old cache. It schedules a task to refresh
cacheWithScheduledFuture.getCached(); // => 1L

final Optional<Future<?>> maybeFuture = cacheWithScheduledFuture.getFuture();
if (maybeFuture.isPresent()) { // If a task is scheduled, `getWithRefreshAheadAsync()` returns Future with cached value
    maybeFuture.get().get(); // sync here (you don't have to do this)
}

cacheWithScheduledFuture = longThinCache.getWithRefreshAheadAsync(); // Hit the new cache, it doesn't schedule a task to refresh
cacheWithScheduledFuture.getCached(); // => 2L

ThinCache#forceGetWithRefreshAheadAsync()

This method retrieves always already cached value. And always schedules a task to refresh cache.

Example:

final ThinCache<Long> longThinCache = new ThinCache<>(10, false, new Supplier<Long>() {
    private long i = 0;

    @Override
    public Long get() {
        return ++i;
    }
});

CacheWithScheduledFuture<Long> cacheWithScheduledFuture = longThinCache.forceGetWithRefreshAheadAsync(); // cache value hasn't been initialized, so initialize cache *synchronously*
cacheWithScheduledFuture.getCached(); // => 1L

cacheWithScheduledFuture = longThinCache.forceGetWithRefreshAheadAsync(); // hit cache, it schedules a task to refresh
cacheWithScheduledFuture.getCached(); // => 1L

Optional<Future<?>> maybeFuture = cacheWithScheduledFuture.getFuture();
if (maybeFuture.isPresent()) { // If a task is scheduled, `forceGetWithRefreshAheadAsync()` returns Future with cached value
    maybeFuture.get().get(); // sync here (you don't have to do this)
}

cacheWithScheduledFuture = longThinCache.forceGetWithRefreshAheadAsync(); // hit cache, it schedules a task to refresh
cacheWithScheduledFuture.getCached(); // => 2L

maybeFuture = cacheWithScheduledFuture.getFuture();
if (maybeFuture.isPresent()) { // If a task is scheduled, `forceGetWithRefreshAheadAsync()` returns Future with cached value
    maybeFuture.get().get(); // sync here (you don't have to do this)
}

cacheWithScheduledFuture = longThinCache.forceGetWithRefreshAheadAsync(); // hit cache, it schedules a task to refresh
cacheWithScheduledFuture.getCached(); // => 3L

Exception case

If cache value hasn't been initialized, these methods behaves in the same as synchronous one.

More information

javadoc.io

Requires

  • Java8 or later

Author

moznion ([email protected])

License

The MIT License (MIT)
Copyright © 2016 moznion, http://moznion.net/ <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Versions

Version
0.4.0
0.3.0
0.2.0
0.1.1
0.1.0
0.0.2
0.0.1