com.ybrikman.ping.scalaapi.dedupe

DedupingCache

Related Doc: package dedupe

class DedupingCache[K, V] extends AnyRef

A cache you can use to de-dupe expensive calculations to ensure that the same calculation happens at most once while processing any incoming request. For example, imagine you get an incoming request to /foo, and to render this page, you have to make a dozen calls to remote services (e.g. a profile service, a search service, etc) using REST over HTTP. You can use the DedupingCache to ensure you don't make the same exact call more than once (e.g. fetch the exact same profile multiple times) by running all the calls through this client. You would use the URL of the REST call as the key and the Future returned from the call as the value:

val remoteUrl = "http://example.com/some/remote/service" val future: Future[Response] = dedupingCache.get(remoteUrl, WS.url(remoteUrl).get())

While processing any incoming request, using the code above ensures that you will not make multiple calls to the exact same remoteUrl; any duplicates will just return a Future object that is already in the cache.

You should only use the DedupingCache for data that is safe to cache. For example, HTTP GET calls are usually safe to cache since they should be idempotent, but HTTP POST calls are not safe to cache. Also, you need to add the CacheFilter to your filter chain so that it can clean up the cache after you're done processing an incoming request. Otherwise, you'll have a memory leak.

K

The type to use for keys. This type must define an equals and hashCode method. For example, if you're making REST over HTTP calls, the HTTP URL is a good key.

V

The type of value that will be returned. For example, if you're making REST over HTTP calls using Play's WS library, a Future[Response] might be a good type for the value.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. DedupingCache
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Instance Constructors

  1. new DedupingCache()

Value Members

  1. final def !=(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  5. def cleanupCacheForRequest(playRequest: RequestHeader): Unit

    Cleanup the cache after you're completely done processing an incoming request.

    Cleanup the cache after you're completely done processing an incoming request. This is necessary to prevent memory leaks. Should only be used by the CacheFilter.

    playRequest

  6. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  7. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  8. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  9. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  10. def get(key: K, valueIfMissing: ⇒ V)(implicit playRequest: RequestHeader): V

    Get the value for key K from the cache.

    Get the value for key K from the cache. If the value is not already in teh cache, use the valueIfMissing function to calculate a value, store it in the cache, and return that value.

    key
    valueIfMissing
    playRequest
    returns

  11. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  12. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  13. def initCacheForRequest(playRequest: RequestHeader): Unit

    Initialize the cache for the given incoming request.

    Initialize the cache for the given incoming request. Should only be used by the CacheFilter.

    playRequest

  14. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  15. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  16. final def notify(): Unit

    Definition Classes
    AnyRef
  17. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  18. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  19. def toString(): String

    Definition Classes
    AnyRef → Any
  20. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  22. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped