-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refs #35: Custom generic serialisation via type class #86
Changes from 2 commits
2dedc9e
33248f1
22de056
cd6dc0e
1cfbe81
da9ad36
ed20a0e
6963d8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
|
||
import com.typesafe.scalalogging.StrictLogging | ||
|
||
import scala.concurrent.duration.Duration | ||
import scala.concurrent.{ ExecutionContext, Await, Future } | ||
import scala.util.Try | ||
import scalacache.serdes.Codec | ||
|
||
package object scalacache extends StrictLogging { | ||
|
||
class TypedApi[V](implicit val scalaCache: ScalaCache) { | ||
class TypedApi[V](implicit val scalaCache: ScalaCache, codec: Codec[V]) { | ||
|
||
def get(keyParts: Any*)(implicit flags: Flags): Future[Option[V]] = getWithKey(toKey(keyParts)) | ||
|
||
|
@@ -126,7 +128,7 @@ package object scalacache extends StrictLogging { | |
* | ||
* @tparam V the type of values that the cache will accept | ||
*/ | ||
def typed[V](implicit scalaCache: ScalaCache) = new TypedApi[V]()(scalaCache) | ||
def typed[V: Codec](implicit scalaCache: ScalaCache) = new TypedApi[V]()(scalaCache, implicitly[Codec[V]]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a parameter to the implicit param list instead of using implicitly. (See slide 84 of http://plastic-idolatry.com/erik/sw2015.pdf) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting. didn't know there was a cost to |
||
|
||
/** | ||
* Get the value corresponding to the given key from the cache. | ||
|
@@ -137,7 +139,7 @@ package object scalacache extends StrictLogging { | |
* @tparam V the type of the corresponding value | ||
* @return the value, if there is one | ||
*/ | ||
def get[V](keyParts: Any*)(implicit scalaCache: ScalaCache, flags: Flags): Future[Option[V]] = | ||
def get[V: Codec](keyParts: Any*)(implicit scalaCache: ScalaCache, flags: Flags): Future[Option[V]] = | ||
typed[V].get(keyParts: _*) | ||
|
||
/** | ||
|
@@ -150,7 +152,7 @@ package object scalacache extends StrictLogging { | |
* @return the value, if there is one | ||
*/ | ||
@deprecated("This method has moved. Please use scalacache.sync.get", "0.7.0") | ||
def getSync[V](keyParts: Any*)(implicit scalaCache: ScalaCache, flags: Flags): Option[V] = | ||
def getSync[V: Codec](keyParts: Any*)(implicit scalaCache: ScalaCache, flags: Flags): Option[V] = | ||
sync.get[V](keyParts: _*) | ||
|
||
/** | ||
|
@@ -163,7 +165,7 @@ package object scalacache extends StrictLogging { | |
* @param ttl Time To Live (optional, if not specified then the entry will be cached indefinitely) | ||
* @tparam V the type of the corresponding value | ||
*/ | ||
def put[V](keyParts: Any*)(value: V, ttl: Option[Duration] = None)(implicit scalaCache: ScalaCache, flags: Flags): Future[Unit] = | ||
def put[V: Codec](keyParts: Any*)(value: V, ttl: Option[Duration] = None)(implicit scalaCache: ScalaCache, flags: Flags): Future[Unit] = | ||
typed[V].put(keyParts: _*)(value, ttl) | ||
|
||
/** | ||
|
@@ -201,7 +203,7 @@ package object scalacache extends StrictLogging { | |
* @tparam V the type of the block's result | ||
* @return the result, either retrived from the cache or returned by the block | ||
*/ | ||
def caching[V](keyParts: Any*)(f: => Future[V])(implicit scalaCache: ScalaCache, flags: Flags, execContext: ExecutionContext = ExecutionContext.global): Future[V] = | ||
def caching[V](keyParts: Any*)(f: => Future[V])(implicit scalaCache: ScalaCache, flags: Flags, execContext: ExecutionContext = ExecutionContext.global, codec: Codec[V]): Future[V] = | ||
typed[V].caching(keyParts: _*)(f) | ||
|
||
/** | ||
|
@@ -223,10 +225,10 @@ package object scalacache extends StrictLogging { | |
* @tparam V the type of the block's result | ||
* @return the result, either retrived from the cache or returned by the block | ||
*/ | ||
def cachingWithTTL[V](keyParts: Any*)(ttl: Duration)(f: => Future[V])(implicit scalaCache: ScalaCache, flags: Flags, execContext: ExecutionContext = ExecutionContext.global): Future[V] = | ||
def cachingWithTTL[V](keyParts: Any*)(ttl: Duration)(f: => Future[V])(implicit scalaCache: ScalaCache, flags: Flags, execContext: ExecutionContext = ExecutionContext.global, codec: Codec[V]): Future[V] = | ||
typed[V].cachingWithTTL(keyParts: _*)(ttl)(f) | ||
|
||
def cachingWithOptionalTTL[V](keyParts: Any*)(optionalTtl: Option[Duration])(f: => Future[V])(implicit scalaCache: ScalaCache, flags: Flags, execContext: ExecutionContext = ExecutionContext.global): Future[V] = | ||
def cachingWithOptionalTTL[V](keyParts: Any*)(optionalTtl: Option[Duration])(f: => Future[V])(implicit scalaCache: ScalaCache, flags: Flags, execContext: ExecutionContext = ExecutionContext.global, codec: Codec[V]): Future[V] = | ||
typed[V].cachingWithOptionalTTL(keyParts: _*)(optionalTtl)(f) | ||
|
||
private def toKey(parts: Seq[Any])(implicit scalaCache: ScalaCache): String = | ||
|
@@ -246,7 +248,7 @@ package object scalacache extends StrictLogging { | |
* @tparam V the type of the corresponding value | ||
* @return the value, if there is one | ||
*/ | ||
def get[V](keyParts: Any*)(implicit scalaCache: ScalaCache, flags: Flags): Option[V] = | ||
def get[V](keyParts: Any*)(implicit scalaCache: ScalaCache, flags: Flags, codec: Codec[V]): Option[V] = | ||
typed[V].sync.get(keyParts: _*) | ||
|
||
/** | ||
|
@@ -263,7 +265,7 @@ package object scalacache extends StrictLogging { | |
* @tparam V the type of the block's result | ||
* @return the result, either retrived from the cache or returned by the block | ||
*/ | ||
def caching[V](keyParts: Any*)(f: => V)(implicit scalaCache: ScalaCache, flags: Flags): V = | ||
def caching[V](keyParts: Any*)(f: => V)(implicit scalaCache: ScalaCache, flags: Flags, codec: Codec[V]): V = | ||
typed[V].sync.caching(keyParts: _*)(f) | ||
|
||
/** | ||
|
@@ -279,7 +281,7 @@ package object scalacache extends StrictLogging { | |
* @tparam V the type of the block's result | ||
* @return the result, either retrived from the cache or returned by the block | ||
*/ | ||
def cachingWithTTL[V](keyParts: Any*)(ttl: Duration)(f: => V)(implicit scalaCache: ScalaCache, flags: Flags): V = | ||
def cachingWithTTL[V](keyParts: Any*)(ttl: Duration)(f: => V)(implicit scalaCache: ScalaCache, flags: Flags, codec: Codec[V]): V = | ||
typed[V].sync.cachingWithTTL(keyParts: _*)(ttl)(f) | ||
|
||
/** | ||
|
@@ -295,7 +297,7 @@ package object scalacache extends StrictLogging { | |
* @tparam V the type of the block's result | ||
* @return the result, either retrived from the cache or returned by the block | ||
*/ | ||
def cachingWithOptionalTTL[V](keyParts: Any*)(optionalTtl: Option[Duration])(f: => V)(implicit scalaCache: ScalaCache, flags: Flags): V = { | ||
def cachingWithOptionalTTL[V](keyParts: Any*)(optionalTtl: Option[Duration])(f: => V)(implicit scalaCache: ScalaCache, flags: Flags, codec: Codec[V]): V = { | ||
optionalTtl match { | ||
case Some(ttl) => typed[V].sync.cachingWithTTL(keyParts: _*)(ttl)(f) | ||
case None => typed[V].sync.caching(keyParts: _*)(f) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please can we make the package
scalacache.serialization
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool done.