the-algorithm/timelineranker/server/src/main/scala/com/twitter/timelineranker/clients/content_features_cache/ContentFeaturesMemcacheBuilder.scala
twitter-team ef4c5eb65e Twitter Recommendation Algorithm
Please note we have force-pushed a new initial commit in order to remove some publicly-available Twitter user information. Note that this process may be required in the future.
2023-03-31 17:36:31 -05:00

40 lines
1.4 KiB
Scala

package com.twitter.timelineranker.clients.content_features_cache
import com.twitter.bijection.Injection
import com.twitter.bijection.scrooge.CompactScalaCodec
import com.twitter.finagle.stats.StatsReceiver
import com.twitter.storehaus.Store
import com.twitter.timelineranker.recap.model.ContentFeatures
import com.twitter.timelines.clients.memcache_common._
import com.twitter.timelines.content_features.{thriftscala => thrift}
import com.twitter.timelines.model.TweetId
import com.twitter.util.Duration
/**
* Content features will be stored by tweetId
*/
class ContentFeaturesMemcacheBuilder(
config: StorehausMemcacheConfig,
ttl: Duration,
statsReceiver: StatsReceiver) {
private[this] val scalaToThriftInjection: Injection[ContentFeatures, thrift.ContentFeatures] =
Injection.build[ContentFeatures, thrift.ContentFeatures](_.toThrift)(
ContentFeatures.tryFromThrift)
private[this] val thriftToBytesInjection: Injection[thrift.ContentFeatures, Array[Byte]] =
CompactScalaCodec(thrift.ContentFeatures)
private[this] implicit val valueInjection: Injection[ContentFeatures, Array[Byte]] =
scalaToThriftInjection.andThen(thriftToBytesInjection)
private[this] val underlyingBuilder =
new MemcacheStoreBuilder[TweetId, ContentFeatures](
config = config,
scopeName = "contentFeaturesCache",
statsReceiver = statsReceiver,
ttl = ttl
)
def build(): Store[TweetId, ContentFeatures] = underlyingBuilder.build()
}