{"action":"created","comment":{"url":"https://api.github.com/repos/chaordic/ignition-core/pulls/comments/85331428","pull_request_review_id":6053020,"id":85331428,"diff_hunk":"@@ -0,0 +1,343 @@\n+package ignition.core.cache\n+\n+import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap\n+import ignition.core.utils.FutureUtils._\n+import ignition.core.utils.DateUtils._\n+import org.joda.time.{DateTime, Period}\n+import org.slf4j.LoggerFactory\n+\n+\n+import scala.concurrent.duration._\n+import scala.concurrent.{ExecutionContext, Future, Promise}\n+import scala.util.{Failure, Success, Try}\n+\n+\n+object ExpiringMultipleLevelCache {\n+  case class TimestampedValue[V](date: DateTime, value: V) {\n+    def hasExpired(ttl: Period, now: DateTime): Boolean = {\n+      date.plus(ttl).isBefore(now)\n+    }\n+  }\n+\n+  trait GenericCache[V] {\n+    def apply(key: String, genValue: () ⇒ Future[V])(implicit ec: ExecutionContext): Future[V]\n+  }\n+\n+  trait LocalCache[V] extends GenericCache[V] {\n+    def get(key: Any): Option[Future[V]]\n+    def set(key: Any, value: V): Unit\n+  }\n+\n+  trait RemoteWritableCache[V] {\n+    def set(key: String, value: V)(implicit ec: ExecutionContext): Future[Unit]\n+    def setLock(key: String, ttl: FiniteDuration): Future[Boolean]\n+  }\n+\n+  trait RemoteReadableCache[V] {\n+    def get(key: String)(implicit ec: ExecutionContext): Future[Option[V]]\n+  }\n+\n+  trait RemoteCacheRW[V] extends RemoteReadableCache[V] with RemoteWritableCache[V]\n+\n+  trait ReporterCallback {\n+    def onCacheMissNothingFound()\n+    def onCacheMissButFoundExpiredLocal()\n+    def onCacheMissButFoundExpiredRemote()\n+    def onRemoteCacheHit()\n+    def onLocalCacheHit()\n+    def onUnexpectedBehaviour()\n+    def onStillTryingToLockOrGet()\n+    def onSuccessfullyRemoteSetValue()\n+    def onRemoteCacheHitAfterGenerating()\n+    def onErrorGeneratingValue(key: String, eLocal: Throwable)\n+    def onLocalError(key: String, e: Throwable)\n+    def onRemoteError(key: String, t: Throwable): Unit\n+    def onRemoteGiveUp(key: String): Unit\n+  }\n+\n+  object NoOpReporter extends ReporterCallback {\n+    override def onCacheMissNothingFound(): Unit = {}\n+    override def onUnexpectedBehaviour(): Unit = {}\n+    override def onSuccessfullyRemoteSetValue(): Unit = {}\n+    override def onRemoteError(key: String, t: Throwable): Unit = {}\n+    override def onRemoteGiveUp(key: String): Unit = {}\n+    override def onLocalError(key: String, e: Throwable): Unit = {}\n+    override def onErrorGeneratingValue(key: String, eLocal: Throwable): Unit = {}\n+    override def onRemoteCacheHitAfterGenerating(): Unit = {}\n+    override def onCacheMissButFoundExpiredRemote(): Unit = {}\n+    override def onStillTryingToLockOrGet(): Unit = {}\n+    override def onLocalCacheHit(): Unit = {}\n+    override def onRemoteCacheHit(): Unit = {}\n+    override def onCacheMissButFoundExpiredLocal(): Unit = {}\n+  }\n+}\n+\n+\n+import ExpiringMultipleLevelCache._\n+\n+\n+case class ExpiringMultipleLevelCache[V](ttl: Period,\n+                                         localCache: LocalCache[TimestampedValue[V]],\n+                                         remoteRW: Option[RemoteCacheRW[TimestampedValue[V]]] = None,\n+                                         reporter: ExpiringMultipleLevelCache.ReporterCallback = ExpiringMultipleLevelCache.NoOpReporter,\n+                                         maxErrorsToRetryOnRemote: Int = 5) extends GenericCache[V] {\n+\n+  private val logger = LoggerFactory.getLogger(getClass)\n+\n+  private val tempUpdate = new ConcurrentLinkedHashMap.Builder[Any, Future[TimestampedValue[V]]].build()\n+\n+  protected def now = DateTime.now\n+\n+  private def timestamp(v: V) = TimestampedValue(now, v)\n+\n+  private def remoteLockKey(key: Any) = s\"$key-emlc-lock\"\n+\n+  private val remoteLockTTL = 5.seconds","path":"src/main/scala/ignition/core/cache/ExpiringMultipleLevelCache.scala","position":95,"original_position":95,"commit_id":"5018bf9b951128e137897d05ecad00989238990c","original_commit_id":"5018bf9b951128e137897d05ecad00989238990c","user":{"login":"fparisotto","id":551145,"avatar_url":"https://avatars.githubusercontent.com/u/551145?v=3","gravatar_id":"","url":"https://api.github.com/users/fparisotto","html_url":"https://github.com/fparisotto","followers_url":"https://api.github.com/users/fparisotto/followers","following_url":"https://api.github.com/users/fparisotto/following{/other_user}","gists_url":"https://api.github.com/users/fparisotto/gists{/gist_id}","starred_url":"https://api.github.com/users/fparisotto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fparisotto/subscriptions","organizations_url":"https://api.github.com/users/fparisotto/orgs","repos_url":"https://api.github.com/users/fparisotto/repos","events_url":"https://api.github.com/users/fparisotto/events{/privacy}","received_events_url":"https://api.github.com/users/fparisotto/received_events","type":"User","site_admin":false},"body":"I think this could be a parameter","created_at":"2016-10-27T13:04:05Z","updated_at":"2016-10-27T13:04:05Z","html_url":"https://github.com/chaordic/ignition-core/pull/98#discussion_r85331428","pull_request_url":"https://api.github.com/repos/chaordic/ignition-core/pulls/98","_links":{"self":{"href":"https://api.github.com/repos/chaordic/ignition-core/pulls/comments/85331428"},"html":{"href":"https://github.com/chaordic/ignition-core/pull/98#discussion_r85331428"},"pull_request":{"href":"https://api.github.com/repos/chaordic/ignition-core/pulls/98"}}},"pull_request":{"url":"https://api.github.com/repos/chaordic/ignition-core/pulls/98","id":90426994,"html_url":"https://github.com/chaordic/ignition-core/pull/98","diff_url":"https://github.com/chaordic/ignition-core/pull/98.diff","patch_url":"https://github.com/chaordic/ignition-core/pull/98.patch","issue_url":"https://api.github.com/repos/chaordic/ignition-core/issues/98","number":98,"state":"open","locked":false,"title":"Multiple level cache: local with remote fallback","user":{"login":"douglaz","id":163706,"avatar_url":"https://avatars.githubusercontent.com/u/163706?v=3","gravatar_id":"","url":"https://api.github.com/users/douglaz","html_url":"https://github.com/douglaz","followers_url":"https://api.github.com/users/douglaz/followers","following_url":"https://api.github.com/users/douglaz/following{/other_user}","gists_url":"https://api.github.com/users/douglaz/gists{/gist_id}","starred_url":"https://api.github.com/users/douglaz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/douglaz/subscriptions","organizations_url":"https://api.github.com/users/douglaz/orgs","repos_url":"https://api.github.com/users/douglaz/repos","events_url":"https://api.github.com/users/douglaz/events{/privacy}","received_events_url":"https://api.github.com/users/douglaz/received_events","type":"User","site_admin":false},"body":"Tests will eventually be added, but this should be very usable right now.","created_at":"2016-10-21T18:55:41Z","updated_at":"2016-10-27T13:04:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a99b0bb02440d2834e91c7619f5d9f7fc79683f0","assignee":{"login":"fparisotto","id":551145,"avatar_url":"https://avatars.githubusercontent.com/u/551145?v=3","gravatar_id":"","url":"https://api.github.com/users/fparisotto","html_url":"https://github.com/fparisotto","followers_url":"https://api.github.com/users/fparisotto/followers","following_url":"https://api.github.com/users/fparisotto/following{/other_user}","gists_url":"https://api.github.com/users/fparisotto/gists{/gist_id}","starred_url":"https://api.github.com/users/fparisotto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fparisotto/subscriptions","organizations_url":"https://api.github.com/users/fparisotto/orgs","repos_url":"https://api.github.com/users/fparisotto/repos","events_url":"https://api.github.com/users/fparisotto/events{/privacy}","received_events_url":"https://api.github.com/users/fparisotto/received_events","type":"User","site_admin":false},"assignees":[{"login":"fparisotto","id":551145,"avatar_url":"https://avatars.githubusercontent.com/u/551145?v=3","gravatar_id":"","url":"https://api.github.com/users/fparisotto","html_url":"https://github.com/fparisotto","followers_url":"https://api.github.com/users/fparisotto/followers","following_url":"https://api.github.com/users/fparisotto/following{/other_user}","gists_url":"https://api.github.com/users/fparisotto/gists{/gist_id}","starred_url":"https://api.github.com/users/fparisotto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fparisotto/subscriptions","organizations_url":"https://api.github.com/users/fparisotto/orgs","repos_url":"https://api.github.com/users/fparisotto/repos","events_url":"https://api.github.com/users/fparisotto/events{/privacy}","received_events_url":"https://api.github.com/users/fparisotto/received_events","type":"User","site_admin":false},{"login":"fernandors87","id":1848907,"avatar_url":"https://avatars.githubusercontent.com/u/1848907?v=3","gravatar_id":"","url":"https://api.github.com/users/fernandors87","html_url":"https://github.com/fernandors87","followers_url":"https://api.github.com/users/fernandors87/followers","following_url":"https://api.github.com/users/fernandors87/following{/other_user}","gists_url":"https://api.github.com/users/fernandors87/gists{/gist_id}","starred_url":"https://api.github.com/users/fernandors87/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fernandors87/subscriptions","organizations_url":"https://api.github.com/users/fernandors87/orgs","repos_url":"https://api.github.com/users/fernandors87/repos","events_url":"https://api.github.com/users/fernandors87/events{/privacy}","received_events_url":"https://api.github.com/users/fernandors87/received_events","type":"User","site_admin":false}],"milestone":null,"commits_url":"https://api.github.com/repos/chaordic/ignition-core/pulls/98/commits","review_comments_url":"https://api.github.com/repos/chaordic/ignition-core/pulls/98/comments","review_comment_url":"https://api.github.com/repos/chaordic/ignition-core/pulls/comments{/number}","comments_url":"https://api.github.com/repos/chaordic/ignition-core/issues/98/comments","statuses_url":"https://api.github.com/repos/chaordic/ignition-core/statuses/5018bf9b951128e137897d05ecad00989238990c","head":{"label":"chaordic:remote_cache","ref":"remote_cache","sha":"5018bf9b951128e137897d05ecad00989238990c","user":{"login":"chaordic","id":1434501,"avatar_url":"https://avatars.githubusercontent.com/u/1434501?v=3","gravatar_id":"","url":"https://api.github.com/users/chaordic","html_url":"https://github.com/chaordic","followers_url":"https://api.github.com/users/chaordic/followers","following_url":"https://api.github.com/users/chaordic/following{/other_user}","gists_url":"https://api.github.com/users/chaordic/gists{/gist_id}","starred_url":"https://api.github.com/users/chaordic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chaordic/subscriptions","organizations_url":"https://api.github.com/users/chaordic/orgs","repos_url":"https://api.github.com/users/chaordic/repos","events_url":"https://api.github.com/users/chaordic/events{/privacy}","received_events_url":"https://api.github.com/users/chaordic/received_events","type":"Organization","site_admin":false},"repo":{"id":21045905,"name":"ignition-core","full_name":"chaordic/ignition-core","owner":{"login":"chaordic","id":1434501,"avatar_url":"https://avatars.githubusercontent.com/u/1434501?v=3","gravatar_id":"","url":"https://api.github.com/users/chaordic","html_url":"https://github.com/chaordic","followers_url":"https://api.github.com/users/chaordic/followers","following_url":"https://api.github.com/users/chaordic/following{/other_user}","gists_url":"https://api.github.com/users/chaordic/gists{/gist_id}","starred_url":"https://api.github.com/users/chaordic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chaordic/subscriptions","organizations_url":"https://api.github.com/users/chaordic/orgs","repos_url":"https://api.github.com/users/chaordic/repos","events_url":"https://api.github.com/users/chaordic/events{/privacy}","received_events_url":"https://api.github.com/users/chaordic/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/chaordic/ignition-core","description":"Ignition-core","fork":true,"url":"https://api.github.com/repos/chaordic/ignition-core","forks_url":"https://api.github.com/repos/chaordic/ignition-core/forks","keys_url":"https://api.github.com/repos/chaordic/ignition-core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/chaordic/ignition-core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/chaordic/ignition-core/teams","hooks_url":"https://api.github.com/repos/chaordic/ignition-core/hooks","issue_events_url":"https://api.github.com/repos/chaordic/ignition-core/issues/events{/number}","events_url":"https://api.github.com/repos/chaordic/ignition-core/events","assignees_url":"https://api.github.com/repos/chaordic/ignition-core/assignees{/user}","branches_url":"https://api.github.com/repos/chaordic/ignition-core/branches{/branch}","tags_url":"https://api.github.com/repos/chaordic/ignition-core/tags","blobs_url":"https://api.github.com/repos/chaordic/ignition-core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/chaordic/ignition-core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/chaordic/ignition-core/git/refs{/sha}","trees_url":"https://api.github.com/repos/chaordic/ignition-core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/chaordic/ignition-core/statuses/{sha}","languages_url":"https://api.github.com/repos/chaordic/ignition-core/languages","stargazers_url":"https://api.github.com/repos/chaordic/ignition-core/stargazers","contributors_url":"https://api.github.com/repos/chaordic/ignition-core/contributors","subscribers_url":"https://api.github.com/repos/chaordic/ignition-core/subscribers","subscription_url":"https://api.github.com/repos/chaordic/ignition-core/subscription","commits_url":"https://api.github.com/repos/chaordic/ignition-core/commits{/sha}","git_commits_url":"https://api.github.com/repos/chaordic/ignition-core/git/commits{/sha}","comments_url":"https://api.github.com/repos/chaordic/ignition-core/comments{/number}","issue_comment_url":"https://api.github.com/repos/chaordic/ignition-core/issues/comments{/number}","contents_url":"https://api.github.com/repos/chaordic/ignition-core/contents/{+path}","compare_url":"https://api.github.com/repos/chaordic/ignition-core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/chaordic/ignition-core/merges","archive_url":"https://api.github.com/repos/chaordic/ignition-core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/chaordic/ignition-core/downloads","issues_url":"https://api.github.com/repos/chaordic/ignition-core/issues{/number}","pulls_url":"https://api.github.com/repos/chaordic/ignition-core/pulls{/number}","milestones_url":"https://api.github.com/repos/chaordic/ignition-core/milestones{/number}","notifications_url":"https://api.github.com/repos/chaordic/ignition-core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/chaordic/ignition-core/labels{/name}","releases_url":"https://api.github.com/repos/chaordic/ignition-core/releases{/id}","deployments_url":"https://api.github.com/repos/chaordic/ignition-core/deployments","created_at":"2014-06-20T17:15:13Z","updated_at":"2016-07-27T13:17:24Z","pushed_at":"2016-10-26T21:35:43Z","git_url":"git://github.com/chaordic/ignition-core.git","ssh_url":"git@github.com:chaordic/ignition-core.git","clone_url":"https://github.com/chaordic/ignition-core.git","svn_url":"https://github.com/chaordic/ignition-core","homepage":null,"size":572,"stargazers_count":3,"watchers_count":3,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":9,"mirror_url":null,"open_issues_count":3,"forks":9,"open_issues":3,"watchers":3,"default_branch":"master"}},"base":{"label":"chaordic:master","ref":"master","sha":"c180e30ffd923bf39ee4a4321f1461aa1109d812","user":{"login":"chaordic","id":1434501,"avatar_url":"https://avatars.githubusercontent.com/u/1434501?v=3","gravatar_id":"","url":"https://api.github.com/users/chaordic","html_url":"https://github.com/chaordic","followers_url":"https://api.github.com/users/chaordic/followers","following_url":"https://api.github.com/users/chaordic/following{/other_user}","gists_url":"https://api.github.com/users/chaordic/gists{/gist_id}","starred_url":"https://api.github.com/users/chaordic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chaordic/subscriptions","organizations_url":"https://api.github.com/users/chaordic/orgs","repos_url":"https://api.github.com/users/chaordic/repos","events_url":"https://api.github.com/users/chaordic/events{/privacy}","received_events_url":"https://api.github.com/users/chaordic/received_events","type":"Organization","site_admin":false},"repo":{"id":21045905,"name":"ignition-core","full_name":"chaordic/ignition-core","owner":{"login":"chaordic","id":1434501,"avatar_url":"https://avatars.githubusercontent.com/u/1434501?v=3","gravatar_id":"","url":"https://api.github.com/users/chaordic","html_url":"https://github.com/chaordic","followers_url":"https://api.github.com/users/chaordic/followers","following_url":"https://api.github.com/users/chaordic/following{/other_user}","gists_url":"https://api.github.com/users/chaordic/gists{/gist_id}","starred_url":"https://api.github.com/users/chaordic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chaordic/subscriptions","organizations_url":"https://api.github.com/users/chaordic/orgs","repos_url":"https://api.github.com/users/chaordic/repos","events_url":"https://api.github.com/users/chaordic/events{/privacy}","received_events_url":"https://api.github.com/users/chaordic/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/chaordic/ignition-core","description":"Ignition-core","fork":true,"url":"https://api.github.com/repos/chaordic/ignition-core","forks_url":"https://api.github.com/repos/chaordic/ignition-core/forks","keys_url":"https://api.github.com/repos/chaordic/ignition-core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/chaordic/ignition-core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/chaordic/ignition-core/teams","hooks_url":"https://api.github.com/repos/chaordic/ignition-core/hooks","issue_events_url":"https://api.github.com/repos/chaordic/ignition-core/issues/events{/number}","events_url":"https://api.github.com/repos/chaordic/ignition-core/events","assignees_url":"https://api.github.com/repos/chaordic/ignition-core/assignees{/user}","branches_url":"https://api.github.com/repos/chaordic/ignition-core/branches{/branch}","tags_url":"https://api.github.com/repos/chaordic/ignition-core/tags","blobs_url":"https://api.github.com/repos/chaordic/ignition-core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/chaordic/ignition-core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/chaordic/ignition-core/git/refs{/sha}","trees_url":"https://api.github.com/repos/chaordic/ignition-core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/chaordic/ignition-core/statuses/{sha}","languages_url":"https://api.github.com/repos/chaordic/ignition-core/languages","stargazers_url":"https://api.github.com/repos/chaordic/ignition-core/stargazers","contributors_url":"https://api.github.com/repos/chaordic/ignition-core/contributors","subscribers_url":"https://api.github.com/repos/chaordic/ignition-core/subscribers","subscription_url":"https://api.github.com/repos/chaordic/ignition-core/subscription","commits_url":"https://api.github.com/repos/chaordic/ignition-core/commits{/sha}","git_commits_url":"https://api.github.com/repos/chaordic/ignition-core/git/commits{/sha}","comments_url":"https://api.github.com/repos/chaordic/ignition-core/comments{/number}","issue_comment_url":"https://api.github.com/repos/chaordic/ignition-core/issues/comments{/number}","contents_url":"https://api.github.com/repos/chaordic/ignition-core/contents/{+path}","compare_url":"https://api.github.com/repos/chaordic/ignition-core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/chaordic/ignition-core/merges","archive_url":"https://api.github.com/repos/chaordic/ignition-core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/chaordic/ignition-core/downloads","issues_url":"https://api.github.com/repos/chaordic/ignition-core/issues{/number}","pulls_url":"https://api.github.com/repos/chaordic/ignition-core/pulls{/number}","milestones_url":"https://api.github.com/repos/chaordic/ignition-core/milestones{/number}","notifications_url":"https://api.github.com/repos/chaordic/ignition-core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/chaordic/ignition-core/labels{/name}","releases_url":"https://api.github.com/repos/chaordic/ignition-core/releases{/id}","deployments_url":"https://api.github.com/repos/chaordic/ignition-core/deployments","created_at":"2014-06-20T17:15:13Z","updated_at":"2016-07-27T13:17:24Z","pushed_at":"2016-10-26T21:35:43Z","git_url":"git://github.com/chaordic/ignition-core.git","ssh_url":"git@github.com:chaordic/ignition-core.git","clone_url":"https://github.com/chaordic/ignition-core.git","svn_url":"https://github.com/chaordic/ignition-core","homepage":null,"size":572,"stargazers_count":3,"watchers_count":3,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":9,"mirror_url":null,"open_issues_count":3,"forks":9,"open_issues":3,"watchers":3,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/chaordic/ignition-core/pulls/98"},"html":{"href":"https://github.com/chaordic/ignition-core/pull/98"},"issue":{"href":"https://api.github.com/repos/chaordic/ignition-core/issues/98"},"comments":{"href":"https://api.github.com/repos/chaordic/ignition-core/issues/98/comments"},"review_comments":{"href":"https://api.github.com/repos/chaordic/ignition-core/pulls/98/comments"},"review_comment":{"href":"https://api.github.com/repos/chaordic/ignition-core/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/chaordic/ignition-core/pulls/98/commits"},"statuses":{"href":"https://api.github.com/repos/chaordic/ignition-core/statuses/5018bf9b951128e137897d05ecad00989238990c"}}}}
{"actor":{"display_login":"fparisotto"}}