Skip to content

Commit 0f27705

Browse files
committed
Added javadoc for dubbo-filter module dubbo github issue 2884 (apache#2921)
1 parent 3863623 commit 0f27705

File tree

18 files changed

+314
-20
lines changed

18 files changed

+314
-20
lines changed

dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/Cache.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,27 @@
1717
package org.apache.dubbo.cache;
1818

1919
/**
20-
* Cache
20+
* Cache interface to support storing and retrieval of value against a lookup key. It has two operation <b>get</b> and <b>put</b>.
21+
* <li><b>put</b>-Storing value against a key.</li>
22+
* <li><b>get</b>-Retrival of object.</li>
23+
* @see org.apache.dubbo.cache.support.lru.LruCache
24+
* @see org.apache.dubbo.cache.support.jcache.JCache
25+
* @see org.apache.dubbo.cache.support.expiring.ExpiringCache
26+
* @see org.apache.dubbo.cache.support.threadlocal.ThreadLocalCache
2127
*/
2228
public interface Cache {
23-
29+
/**
30+
* API to store value against a key
31+
* @param key Unique identifier for the object being store.
32+
* @param value Value getting store
33+
*/
2434
void put(Object key, Object value);
2535

36+
/**
37+
* API to return stored value using a key.
38+
* @param key Unique identifier for cache lookup
39+
* @return Return stored object against key
40+
*/
2641
Object get(Object key);
2742

2843
}

dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/CacheFactory.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,21 @@
2222
import org.apache.dubbo.rpc.Invocation;
2323

2424
/**
25-
* CacheFactory
25+
* Interface needs to be implemented by all the cache store provider.Along with implementing <b>CacheFactory</b> interface
26+
* entry needs to be added in org.apache.dubbo.cache.CacheFactory file in a classpath META-INF sub directories.
27+
*
28+
* @see Cache
2629
*/
2730
@SPI("lru")
2831
public interface CacheFactory {
2932

33+
/**
34+
* CacheFactory implementation class needs to implement this return underlying cache instance for method against
35+
* url and invocation.
36+
* @param url
37+
* @param invocation
38+
* @return Instance of Cache containing cached value against method url and invocation.
39+
*/
3040
@Adaptive("cache")
3141
Cache getCache(URL url, Invocation invocation);
3242

dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/filter/CacheFilter.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,60 @@
3232
import java.io.Serializable;
3333

3434
/**
35-
* CacheFilter
35+
* CacheFilter is a core component of dubbo.Enabling <b>cache</b> key of service,method,consumer or provider dubbo will cache method return value.
36+
* Along with cache key we need to configure cache type. Dubbo default implemented cache types are
37+
* <li>lur</li>
38+
* <li>threadlocal</li>
39+
* <li>jcache</li>
40+
* <li>expiring</li>
41+
*
42+
* <pre>
43+
* e.g. 1)&lt;dubbo:service cache="lru" /&gt;
44+
* 2)&lt;dubbo:service /&gt; &lt;dubbo:method name="method2" cache="threadlocal" /&gt; &lt;dubbo:service/&gt;
45+
* 3)&lt;dubbo:provider cache="expiring" /&gt;
46+
* 4)&lt;dubbo:consumer cache="jcache" /&gt;
47+
*
48+
*If cache type is defined in method level then method level type will get precedence. According to above provided
49+
*example, if service has two method, method1 and method2, method2 will have cache type as <b>threadlocal</b> where others will
50+
*be backed by <b>lru</b>
51+
*</pre>
52+
*
53+
* @see org.apache.dubbo.rpc.Filter
54+
* @see org.apache.dubbo.cache.support.lru.LruCacheFactory
55+
* @see org.apache.dubbo.cache.support.lru.LruCache
56+
* @see org.apache.dubbo.cache.support.jcache.JCacheFactory
57+
* @see org.apache.dubbo.cache.support.jcache.JCache
58+
* @see org.apache.dubbo.cache.support.threadlocal.ThreadLocalCacheFactory
59+
* @see org.apache.dubbo.cache.support.threadlocal.ThreadLocalCache
60+
* @see org.apache.dubbo.cache.support.expiring.ExpiringCacheFactory
61+
* @see org.apache.dubbo.cache.support.expiring.ExpiringCache
62+
*
3663
*/
3764
@Activate(group = {Constants.CONSUMER, Constants.PROVIDER}, value = Constants.CACHE_KEY)
3865
public class CacheFilter implements Filter {
3966

4067
private CacheFactory cacheFactory;
4168

69+
/**
70+
* Dubbo will populate and set the cache factory instance based on service/method/consumer/provider configured
71+
* cache attribute value. Dubbo will search for the class name implementing configured <b>cache</b> in file org.apache.dubbo.cache.CacheFactory
72+
* under META-INF sub folders.
73+
*
74+
* @param cacheFactory instance of CacheFactory based on <b>cache</b> type
75+
*/
4276
public void setCacheFactory(CacheFactory cacheFactory) {
4377
this.cacheFactory = cacheFactory;
4478
}
4579

80+
/**
81+
* If cache is configured, dubbo will invoke method on each method call. If cache value is returned by cache store
82+
* then it will return otherwise call the remote method and return value. If remote method's return valeu has error
83+
* then it will not cache the value.
84+
* @param invoker service
85+
* @param invocation invocation.
86+
* @return Cache returned value if found by the underlying cache store. If cache miss it will call target method.
87+
* @throws RpcException
88+
*/
4689
@Override
4790
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
4891
if (cacheFactory != null && ConfigUtils.isNotEmpty(invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.CACHE_KEY))) {
@@ -66,7 +109,10 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
66109
}
67110
return invoker.invoke(invocation);
68111
}
69-
112+
113+
/**
114+
* Cache value wrapper.
115+
*/
70116
static class ValueWrapper implements Serializable{
71117

72118
private static final long serialVersionUID = -1777337318019193256L;

dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/support/AbstractCacheFactory.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,29 @@
2626
import java.util.concurrent.ConcurrentMap;
2727

2828
/**
29-
* AbstractCacheFactory
29+
* AbstractCacheFactory is a default implementation of {@link CacheFactory}. It abstract out the key formation from URL along with
30+
* invocation method. It initially check if the value for key already present in own local in-memory store then it won't check underlying storage cache {@link Cache}.
31+
* Internally it used {@link ConcurrentHashMap} to store do level-1 caching.
32+
*
33+
* @see CacheFactory
34+
* @see org.apache.dubbo.cache.support.jcache.JCacheFactory
35+
* @see org.apache.dubbo.cache.support.lru.LruCacheFactory
36+
* @see org.apache.dubbo.cache.support.threadlocal.ThreadLocalCacheFactory
37+
* @see org.apache.dubbo.cache.support.expiring.ExpiringCacheFactory
3038
*/
3139
public abstract class AbstractCacheFactory implements CacheFactory {
3240

41+
/**
42+
* This is used to store factory level-1 cached data.
43+
*/
3344
private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<String, Cache>();
3445

46+
/**
47+
* Takes URL and invocation instance and return cache instance for a given url.
48+
* @param url url of the method
49+
* @param invocation invocation context.
50+
* @return Instance of cache store used as storage for caching return values.
51+
*/
3552
@Override
3653
public Cache getCache(URL url, Invocation invocation) {
3754
url = url.addParameter(Constants.METHOD_KEY, invocation.getMethodName());
@@ -44,6 +61,11 @@ public Cache getCache(URL url, Invocation invocation) {
4461
return cache;
4562
}
4663

64+
/**
65+
* Takes url as an method argument and return new instance of cache store implemented by AbstractCacheFactory subclass.
66+
* @param url url of the method
67+
* @return Create and return new instance of cache store used as storage for caching return values.
68+
*/
4769
protected abstract Cache createCache(URL url);
4870

4971
}

dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/support/expiring/ExpiringCache.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@
2424
/**
2525
* ExpiringCache - With the characteristic of expiration time.
2626
*/
27+
28+
/**
29+
* This class store the cache value with the characteristic of expiration time. If a service,method,consumer or provided is configured with key <b>cache</b>
30+
* with value <b>expiring</b>, dubbo initialize the instance of this class using {@link ExpiringCacheFactory} to store method's returns value
31+
* to server from store without making method call.
32+
* <pre>
33+
* e.g. 1) &lt;dubbo:service cache="expiring" cache.seconds="60" cache.interval="10"/&gt;
34+
* 2) &lt;dubbo:consumer cache="expiring" /&gt;
35+
* </pre>
36+
* <li>It used constructor argument url instance <b>cache.seconds</b> value to decide time to live of cached object.Default value of it is 180 second.</li>
37+
* <li>It used constructor argument url instance <b>cache.interval</b> value for cache value expiration interval.Default value of this is 4 second</li>
38+
* @see Cache
39+
* @see ExpiringCacheFactory
40+
* @see org.apache.dubbo.cache.support.AbstractCacheFactory
41+
* @see org.apache.dubbo.cache.filter.CacheFilter
42+
*/
2743
public class ExpiringCache implements Cache {
2844
private final Map<Object, Object> store;
2945

@@ -37,11 +53,22 @@ public ExpiringCache(URL url) {
3753
this.store = expiringMap;
3854
}
3955

56+
/**
57+
* API to store value against a key in the calling thread scope.
58+
* @param key Unique identifier for the object being store.
59+
* @param value Value getting store
60+
*/
4061
@Override
4162
public void put(Object key, Object value) {
4263
store.put(key, value);
4364
}
4465

66+
/**
67+
* API to return stored value using a key against the calling thread specific store.
68+
* @param key Unique identifier for cache lookup
69+
* @return Return stored object against key
70+
*/
71+
4572
@Override
4673
public Object get(Object key) {
4774
return store.get(key);

dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/support/expiring/ExpiringCacheFactory.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,23 @@
2020
import org.apache.dubbo.cache.support.AbstractCacheFactory;
2121
import org.apache.dubbo.common.URL;
2222

23+
2324
/**
24-
* ExpiringCacheFactory
25+
* Implement {@link org.apache.dubbo.cache.CacheFactory} by extending {@link AbstractCacheFactory} and provide
26+
* instance of new {@link ExpiringCache}.
27+
*
28+
* @see AbstractCacheFactory
29+
* @see ExpiringCache
30+
* @see Cache
2531
*/
32+
2633
public class ExpiringCacheFactory extends AbstractCacheFactory {
27-
34+
35+
/**
36+
* Takes url as an method argument and return new instance of cache store implemented by JCache.
37+
* @param url url of the method
38+
* @return ExpiringCache instance of cache
39+
*/
2840
@Override
2941
protected Cache createCache(URL url) {
3042
return new ExpiringCache(url);

dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/support/jcache/JCache.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@
3030
import java.util.concurrent.TimeUnit;
3131

3232
/**
33-
* JCache
33+
* This class store the cache value per thread. If a service,method,consumer or provided is configured with key <b>cache</b>
34+
* with value <b>jcache</b>, dubbo initialize the instance of this class using {@link JCacheFactory} to store method's returns value
35+
* to server from store without making method call.
36+
*
37+
* @see Cache
38+
* @see JCacheFactory
39+
* @see org.apache.dubbo.cache.support.AbstractCacheFactory
40+
* @see org.apache.dubbo.cache.filter.CacheFilter
3441
*/
3542
public class JCache implements org.apache.dubbo.cache.Cache {
3643

dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/support/jcache/JCacheFactory.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,26 @@
2020
import org.apache.dubbo.cache.support.AbstractCacheFactory;
2121
import org.apache.dubbo.common.URL;
2222

23+
import javax.cache.spi.CachingProvider;
24+
2325
/**
24-
* JCacheFactory
26+
* JCacheFactory is factory class to provide instance of javax spi cache.Implement {@link org.apache.dubbo.cache.CacheFactory} by
27+
* extending {@link AbstractCacheFactory} and provide
28+
* @see AbstractCacheFactory
29+
* @see JCache
30+
* @see org.apache.dubbo.cache.filter.CacheFilter
31+
* @see Cache
32+
* @see CachingProvider
33+
* @see javax.cache.Cache
34+
* @see javax.cache.CacheManager
2535
*/
2636
public class JCacheFactory extends AbstractCacheFactory {
2737

38+
/**
39+
* Takes url as an method argument and return new instance of cache store implemented by JCache.
40+
* @param url url of the method
41+
* @return JCache instance of cache
42+
*/
2843
@Override
2944
protected Cache createCache(URL url) {
3045
return new JCache(url);

dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/support/lru/LruCache.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,55 @@
2323
import java.util.Map;
2424

2525
/**
26-
* LruCache
26+
* This class store the cache value per thread. If a service,method,consumer or provided is configured with key <b>cache</b>
27+
* with value <b>lru</b>, dubbo initialize the instance of this class using {@link LruCacheFactory} to store method's returns value
28+
* to server from store without making method call.
29+
* <pre>
30+
* e.g. 1) &lt;dubbo:service cache="lru" cache.size="5000"/&gt;
31+
* 2) &lt;dubbo:consumer cache="lru" /&gt;
32+
* </pre>
33+
* <pre>
34+
* LruCache uses url's <b>cache.size</b> value for its max store size, if nothing is provided then
35+
* default value will be 1000
36+
* </pre>
37+
*
38+
* @see Cache
39+
* @see LruCacheFactory
40+
* @see org.apache.dubbo.cache.support.AbstractCacheFactory
41+
* @see org.apache.dubbo.cache.filter.CacheFilter
2742
*/
2843
public class LruCache implements Cache {
2944

45+
/**
46+
* This is used to store cache records
47+
*/
3048
private final Map<Object, Object> store;
3149

50+
/**
51+
* Initialize LruCache, it uses constructor argument <b>cache.size</b> value as its storage max size.
52+
* If nothing is provided then it will use 1000 as default value.
53+
* @param url A valid URL instance
54+
*/
3255
public LruCache(URL url) {
3356
final int max = url.getParameter("cache.size", 1000);
3457
this.store = new LRUCache<Object, Object>(max);
3558
}
3659

60+
/**
61+
* API to store value against a key in the calling thread scope.
62+
* @param key Unique identifier for the object being store.
63+
* @param value Value getting store
64+
*/
3765
@Override
3866
public void put(Object key, Object value) {
3967
store.put(key, value);
4068
}
4169

70+
/**
71+
* API to return stored value using a key against the calling thread specific store.
72+
* @param key Unique identifier for cache lookup
73+
* @return Return stored object against key
74+
*/
4275
@Override
4376
public Object get(Object key) {
4477
return store.get(key);

dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/support/lru/LruCacheFactory.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,20 @@
2121
import org.apache.dubbo.common.URL;
2222

2323
/**
24-
* LruCacheFactory
24+
* Implement {@link org.apache.dubbo.cache.CacheFactory} by extending {@link AbstractCacheFactory} and provide
25+
* instance of new {@link LruCache}.
26+
*
27+
* @see AbstractCacheFactory
28+
* @see LruCache
29+
* @see Cache
2530
*/
2631
public class LruCacheFactory extends AbstractCacheFactory {
2732

33+
/**
34+
* Takes url as an method argument and return new instance of cache store implemented by LruCache.
35+
* @param url url of the method
36+
* @return ThreadLocalCache instance of cache
37+
*/
2838
@Override
2939
protected Cache createCache(URL url) {
3040
return new LruCache(url);

0 commit comments

Comments
 (0)