Skip to content

Commit

Permalink
Completed and refactored the Cache components.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Dec 11, 2016
1 parent 080b82d commit 28f04a8
Show file tree
Hide file tree
Showing 13 changed files with 466 additions and 296 deletions.
11 changes: 8 additions & 3 deletions rapidoid-commons/src/main/java/org/rapidoid/cache/Cache.java
Expand Up @@ -23,17 +23,22 @@
import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.cache.impl.CacheDSL;
import org.rapidoid.cache.impl.ConcurrentCacheAtom;
import org.rapidoid.lambda.Mapper;

import java.util.concurrent.Callable;

@Authors("Nikolche Mihajlovski")
@Since("5.3.0")
public class Cache extends RapidoidThing {

public static <K, V> CacheDSL<K, V> of(org.rapidoid.lambda.Mapper<K, V> of) {
public static <K, V> CacheDSL<K, V> of(Mapper<K, V> of) {
return new CacheDSL<K, V>().of(of);
}

public static <K, V> Cached<K, V> none() {
return new NotCached<>();
public static <T> CacheAtom<T> atom(Callable<T> loader, long ttlInMs) {
return new ConcurrentCacheAtom<>(loader, ttlInMs);
}

}
Expand Up @@ -25,16 +25,26 @@

@Authors("Nikolche Mihajlovski")
@Since("5.3.0")
public interface CachedCalc<V> {
public interface CacheAtom<V> {

/**
* Returns the cached value, recalculating/reloading it if expired.
*/
V get();

/**
* Invalidates the cache.
* Retrieves the cached value if it exists, or <code>null</code> otherwise.
*/
V getIfExists();

/**
* Invalidates the cached value.
*/
void invalidate();

/**
* Sets a new cached value.
*/
void set(V value);

}
16 changes: 16 additions & 0 deletions rapidoid-commons/src/main/java/org/rapidoid/cache/Cached.java
Expand Up @@ -27,8 +27,24 @@
@Since("5.3.0")
public interface Cached<K, V> {

/**
* Returns the cached value for the given key, recalculating/reloading it if expired.
*/
V get(K key);

/**
* Retrieves the cached value for the given key if it exists, or <code>null</code> otherwise.
*/
V getIfExists(K key);

/**
* Invalidates the cached value for the given key.
*/
void invalidate(K key);

/**
* Sets a new cached value for the given key.
*/
void set(K key, V value);

}
87 changes: 0 additions & 87 deletions rapidoid-commons/src/main/java/org/rapidoid/cache/CachedImpl.java

This file was deleted.

This file was deleted.

40 changes: 0 additions & 40 deletions rapidoid-commons/src/main/java/org/rapidoid/cache/NotCached.java

This file was deleted.

@@ -1,4 +1,4 @@
package org.rapidoid.cache;
package org.rapidoid.cache.impl;

/*
* #%L
Expand All @@ -23,6 +23,7 @@
import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.cache.Cached;

@Authors("Nikolche Mihajlovski")
@Since("5.3.0")
Expand Down
@@ -1,4 +1,4 @@
package org.rapidoid.cache;
package org.rapidoid.cache.impl;

/*
* #%L
Expand Down Expand Up @@ -28,8 +28,8 @@
@Since("5.3.0")
public class CacheFactory extends RapidoidThing {

public static <K, V> CachedImpl<K, V> create(CacheDSL<K, V> params) {
return new CachedImpl<K, V>(params.capacity(), params.of(), params.ttl());
public static <K, V> ConcurrentCached<K, V> create(CacheDSL<K, V> params) {
return new ConcurrentCached<K, V>(params.capacity(), params.of(), params.ttl());
}

}

0 comments on commit 28f04a8

Please sign in to comment.