Skip to content

Commit

Permalink
Added API for emptying the cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Feb 2, 2017
1 parent cfc59d3 commit 03728ed
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 4 additions & 0 deletions rapidoid-commons/src/main/java/org/rapidoid/cache/Cache.java
Expand Up @@ -47,4 +47,8 @@ public interface Cache<K, V> {
*/ */
void set(K key, V value); void set(K key, V value);


/**
* Clears the cache.
*/
void clear();
} }
Expand Up @@ -34,11 +34,11 @@ public abstract class AbstractMapImpl<K, V> extends RapidoidThing implements Sim
protected V defaultValue; protected V defaultValue;


public AbstractMapImpl(int width) { public AbstractMapImpl(int width) {
this.entries = new SimpleHashTable<MapEntry<K, V>>(width); this.entries = new SimpleHashTable<>(width);
} }


public AbstractMapImpl(int width, int initialBucketSize) { public AbstractMapImpl(int width, int initialBucketSize) {
this.entries = new SimpleHashTable<MapEntry<K, V>>(width, initialBucketSize); this.entries = new SimpleHashTable<>(width, initialBucketSize);
} }


@Override @Override
Expand Down
Expand Up @@ -3,12 +3,12 @@
import org.rapidoid.RapidoidThing; import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors; import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since; import org.rapidoid.annotation.Since;
import org.rapidoid.collection.Coll; import org.rapidoid.cache.Cache;
import org.rapidoid.cache.Caching;
import org.rapidoid.env.Env;
import org.rapidoid.lambda.Mapper; import org.rapidoid.lambda.Mapper;
import org.rapidoid.u.U; import org.rapidoid.u.U;


import java.util.Map;

/* /*
* #%L * #%L
* rapidoid-render * rapidoid-render
Expand All @@ -33,12 +33,14 @@
@Since("5.2.0") @Since("5.2.0")
public class RapidoidTemplateFactory extends RapidoidThing implements TemplateFactory { public class RapidoidTemplateFactory extends RapidoidThing implements TemplateFactory {


private final Map<String, RapidoidTemplate> compiledTemplates = Coll.autoExpandingMap(new Mapper<String, RapidoidTemplate>() { private static final int CACHE_TTL = Env.dev() ? 300 : 1000;

private final Cache<String, RapidoidTemplate> compiledTemplates = Caching.of(new Mapper<String, RapidoidTemplate>() {
@Override @Override
public RapidoidTemplate map(String filename) throws Exception { public RapidoidTemplate map(String filename) throws Exception {
return loadAndCompile(filename); return loadAndCompile(filename);
} }
}); }).capacity(10000).ttl(CACHE_TTL).build();


public RapidoidTemplate loadAndCompile(String filename) { public RapidoidTemplate loadAndCompile(String filename) {
return new RapidoidTemplate(filename, loadTemplate(filename), this); return new RapidoidTemplate(filename, loadTemplate(filename), this);
Expand Down

0 comments on commit 03728ed

Please sign in to comment.