@@ -3,15 +3,12 @@ package com.redbubble.util.cache.memory
3
3
import java .util .concurrent .{Executor , TimeUnit }
4
4
5
5
import com .github .benmanes .caffeine .cache .Caffeine
6
- import com .redbubble .util .async .syntax ._
7
- import com .redbubble .util .cache .{CacheKey , SimpleCache }
6
+ import com .redbubble .util .cache .{CacheKey , Caching , SimpleCache }
8
7
import com .redbubble .util .metrics .StatsReceiver
9
8
import com .twitter .util .{Duration , Future }
10
9
11
- import scala .concurrent .ExecutionContext .fromExecutor
12
- import scala .concurrent .duration .{Duration => ScalaDuration }
13
10
import scalacache .serialization .{Codec , InMemoryRepr }
14
- import scalacache .{CacheConfig , Flags , ScalaCache }
11
+ import scalacache .{CacheConfig , ScalaCache }
15
12
16
13
/**
17
14
* An in-memory cache, with metrics tracking.
@@ -24,33 +21,30 @@ import scalacache.{CacheConfig, Flags, ScalaCache}
24
21
*/
25
22
private [cache] final class InMemorySimpleCache (name : String , maxSize : Long , ttl : Duration )
26
23
(implicit ex : Executor , statsReceiver : StatsReceiver ) extends SimpleCache {
27
- private val flags = Flags (readsEnabled = true , writesEnabled = true )
28
- private val scalaTtl = ScalaDuration (ttl.inNanoseconds, TimeUnit .NANOSECONDS )
29
- private val cache = createCache(name, maxSize, scalaTtl, ex, statsReceiver)
30
- private val ec = fromExecutor(ex)
24
+ private val cache = createCache(name, maxSize, ex, statsReceiver)
31
25
32
26
override def caching [V ](key : CacheKey )(f : => Future [V ]): Future [V ] = {
33
- val noOpCodec = Codec .anyToNoSerialization[V ]
34
- scalacache.cachingWithTTL[ V , InMemoryRepr ](key)(scalaTtl)(f.asScala)( cache, flags, ec, noOpCodec).asTwitter(ec )
27
+ val codec = Codec .anyToNoSerialization[V ]
28
+ Caching .caching( cache, ttl, key, codec)(f)(ex )
35
29
}
36
30
37
31
override def put [V , Repr ](key : CacheKey , value : V ): Future [Unit ] = {
38
- val noOpCodec = Codec .anyToNoSerialization[V ]
39
- scalacache .put[ V , InMemoryRepr ](key)(value, Some (scalaTtl))(cache, flags, noOpCodec).asTwitter(ec )
32
+ val codec = Codec .anyToNoSerialization[V ]
33
+ Caching .put(cache, ttl, key, codec, value)(ex )
40
34
}
41
35
42
36
override def get [V ](key : CacheKey ): Future [Option [V ]] = {
43
- val noOpCodec = Codec .anyToNoSerialization[V ]
44
- scalacache .get[ V , InMemoryRepr ](key)( cache, flags, noOpCodec).asTwitter(ec )
37
+ val codec = Codec .anyToNoSerialization[V ]
38
+ Caching .get( cache, key, codec)(ex )
45
39
}
46
40
47
- override def flush (): Future [Unit ] = cache.cache.removeAll().asTwitter(ec )
41
+ override def flush (): Future [Unit ] = Caching .flush(cache)(ex )
48
42
49
43
private def createCache (
50
- name : String , maxSize : Long , ttl : ScalaDuration , executor : Executor , statsReceiver : StatsReceiver ): ScalaCache [InMemoryRepr ] = {
44
+ name : String , maxSize : Long , executor : Executor , statsReceiver : StatsReceiver ): ScalaCache [InMemoryRepr ] = {
51
45
val underlying = Caffeine .newBuilder()
52
46
.maximumSize(maxSize)
53
- .expireAfterWrite(ttl.length, ttl.unit )
47
+ .expireAfterWrite(ttl.inNanoseconds, TimeUnit . NANOSECONDS )
54
48
.executor(executor)
55
49
.recordStats(() => new StatsCounter (name, statsReceiver))
56
50
.build[String , Object ]
0 commit comments