com.h3c.util.cache

## Interface Cache<K,V>

- Type Parameters:
    `K` - type of the key to identify a cached value. It should be an immutable type. It is critical this type implements equals() and hashCode() correctly.
    `V` - type of the cached value.
  All Known Implementing Classes:
    CacheModelProxy, SmallDataCache

      public interface Cache<K,V>

  In computer science, a cache is a component that transparently stores data so that future requests for that data can be served faster. The data that is stored within a cache might be values that have been computed earlier or duplicates of original values that are stored elsewhere. If requested data is contained in the cache (cache hit), this request can be served by simply reading the cache, which is comparatively faster. Otherwise (cache miss), the data has to be recomputed or fetched from its original storage location, which is comparatively slower. Hence, the greater the number of requests that can be served from the cache, the faster the overall system performance becomes.

  Caches usually makes temporal and/or spatial considerations to clear values.

  Use the proxy pattern to incorporate a cache. The proxy would get values from the cache and it would delegate in case the value is not contained in the cache. Example: ``

       public interface MyService {
           public MyCacheable getCacheable(MyKey key);
       }

       public class MyServiceCacheProxy implements MyService {
           private MyService delegate;
           private Cache cache;

           public MyServiceCacheProxy(MyService subject) {
               this.delegate = subject;
               this.cache = ...;
           }

           @Override
           public MyCacheable getCacheable(MyKey key) {
               MyCacheable result = this.cache.get(key);
               if(result == null) {
                   result = this.delegate.getCacheable(key);
                   if(result != null) {
                       this.cache.put(key, result);
                   }
               }
               return result;
           }
       }

- - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `clear()` Clears the cache.
    `V` | `get(K key)` Gets a value from the cache.
    `void` | `invalidate(K key)` Invalidates a key.
    `void` | `put(K key, V value)` Puts a value into the cache.

- - ### Method Detail

    - #### put

          void put(K key,
                 V value)

      Puts a value into the cache.
      Parameters:
        `key` - key.
        `value` - value.

    - #### get

          V get(K key)

      Gets a value from the cache.
      Parameters:
        `key` - value's key.
      Returns:
        the value associated to the given key if it exists in the cache, `null` otherwise.

    - #### invalidate

          void invalidate(K key)

      Invalidates a key. This will cause the key to be removed from the cache. This method should be called when the object associated to the given key is no longer valid (A new copy should be retrieved from the model in the next read or the object no longer exists in the model). Deletion of entries from the cache due space management should be handled by the cache's implementation.
      Parameters:
        `key` - key to invalidate.

    - #### clear

          void clear()

      Clears the cache.

Copyright © 2014. All Rights Reserved.