Skip to content

Commit 9f28930

Browse files
committed
Cache map interface and default implementation
1 parent d6b507e commit 9f28930

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 2016 The original author or authors
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License v1.0
6+
* and Apache License v2.0 which accompanies this distribution.
7+
*
8+
* The Eclipse Public License is available at
9+
* http://www.eclipse.org/legal/epl-v10.html
10+
*
11+
* The Apache License v2.0 is available at
12+
* http://www.opensource.org/licenses/apache2.0.php
13+
*
14+
* You may elect to redistribute this code under either of these licenses.
15+
*/
16+
17+
package io.engagingspaces.vertx.dataloader;
18+
19+
import io.engagingspaces.vertx.dataloader.impl.DefaultCacheMap;
20+
21+
/**
22+
* Cache map interface for data loaders that use caching.
23+
* <p>
24+
* The default implementation used by the data loader is based on a {@link java.util.LinkedHashMap}. Note that the
25+
* implementation could also have used a regular {@link java.util.Map} instead of this {@link CacheMap}, but
26+
* this aligns better to the reference data loader implementation provided by Facebook
27+
* <p>
28+
* Also it doesn't require you to implement the full set of map overloads, just the required methods.
29+
*
30+
* @param <U> type parameter indicating the type of the cache keys
31+
* @param <V> type parameter indicating the type of the data that is cached
32+
*
33+
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
34+
*/
35+
public interface CacheMap<U, V> {
36+
37+
/**
38+
* Creates a new cache map, using the default implementation that is based on a {@link java.util.LinkedHashMap}.
39+
*
40+
* @param <U> type parameter indicating the type of the cache keys
41+
* @param <V> type parameter indicating the type of the data that is cached
42+
* @return the cache map
43+
*/
44+
static <U, V> CacheMap<U, V> simpleMap() {
45+
return new DefaultCacheMap<>();
46+
}
47+
48+
/**
49+
* Checks whether the specified key is contained in the cach map.
50+
*
51+
* @param key the key to check
52+
* @return {@code true} if the cache contains the key, {@code false} otherwise
53+
*/
54+
boolean containsKey(U key);
55+
56+
/**
57+
* Gets the specified key from the cache map.
58+
* <p>
59+
* May throw an exception if the key does not exists, depending on the cache map implementation that is used,
60+
* so be sure to check {@link CacheMap#containsKey(Object)} first.
61+
*
62+
* @param key the key to retrieve
63+
* @return the cached value, or {@code null} if not found (depends on cache implementation)
64+
*/
65+
V get(U key);
66+
67+
/**
68+
* Creates a new cache map entry with the specified key and value, or updates the value if the key already exists.
69+
*
70+
* @param key the key to cache
71+
* @param value the value to cache
72+
* @return the cache map for fluent coding
73+
*/
74+
CacheMap set(U key, V value);
75+
76+
/**
77+
* Deletes the entry with the specified key from the cache map, if it exists.
78+
*
79+
* @param key the key to delete
80+
* @return the cache map for fluent coding
81+
*/
82+
CacheMap delete(U key);
83+
84+
/**
85+
* Clears all entries of the cache map
86+
*
87+
* @return the cache map for fluent coding
88+
*/
89+
CacheMap clear();
90+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2016 The original author or authors
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License v1.0
6+
* and Apache License v2.0 which accompanies this distribution.
7+
*
8+
* The Eclipse Public License is available at
9+
* http://www.eclipse.org/legal/epl-v10.html
10+
*
11+
* The Apache License v2.0 is available at
12+
* http://www.opensource.org/licenses/apache2.0.php
13+
*
14+
* You may elect to redistribute this code under either of these licenses.
15+
*/
16+
17+
package io.engagingspaces.vertx.dataloader.impl;
18+
19+
import io.engagingspaces.vertx.dataloader.CacheMap;
20+
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
24+
/**
25+
*
26+
* @param <K>
27+
* @param <V>
28+
*
29+
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
30+
*/
31+
public class DefaultCacheMap<K, V> implements CacheMap<K, V> {
32+
33+
private Map<K, V> cache;
34+
35+
/**
36+
* Default constructor
37+
*/
38+
public DefaultCacheMap() {
39+
cache = new HashMap<>();
40+
}
41+
42+
/**
43+
* {@inheritDoc}
44+
*/
45+
@Override
46+
public boolean containsKey(K key) {
47+
return cache.containsKey(key);
48+
}
49+
50+
/**
51+
* {@inheritDoc}
52+
*/
53+
@Override
54+
public V get(K key) {
55+
return cache.get(key);
56+
}
57+
58+
/**
59+
* {@inheritDoc}
60+
*/
61+
@Override
62+
public CacheMap set(K key, V value) {
63+
cache.put(key, value);
64+
return this;
65+
}
66+
67+
/**
68+
* {@inheritDoc}
69+
*/
70+
@Override
71+
public CacheMap delete(K key) {
72+
cache.remove(key);
73+
return this;
74+
}
75+
76+
/**
77+
* {@inheritDoc}
78+
*/
79+
@Override
80+
public CacheMap clear() {
81+
cache.clear();
82+
return this;
83+
}
84+
}

0 commit comments

Comments
 (0)