faster cache update on follow/unfollow

This commit is contained in:
Thibault Duplessis 2017-04-04 01:31:33 +02:00
parent aba7588b23
commit eac931d1a0
2 changed files with 9 additions and 4 deletions

View file

@ -28,6 +28,11 @@ final class AsyncCacheClearable[K, V](
}
})
def update(k: K, f: V => V): Unit =
cache.getIfPresent(k) foreach { fu =>
cache.put(k, fu map f)
}
def invalidate(k: K): Unit = cache invalidate k
def invalidateAll: Unit = cache.invalidateAll

View file

@ -114,8 +114,8 @@ final class RelationApi(
case (Some(Follow), _) => funit
case (_, Some(Block)) => funit
case _ => RelationRepo.follow(u1, u2) >> limitFollow(u1) >>- {
countFollowersCache invalidate u2
countFollowingCache invalidate u1
countFollowersCache.update(u2, 1+)
countFollowingCache.update(u1, 1+)
reloadOnlineFriends(u1, u2)
timeline ! Propagate(FollowUser(u1, u2)).toFriendsOf(u1).toUsers(List(u2))
lila.mon.relation.follow()
@ -147,8 +147,8 @@ final class RelationApi(
def unfollow(u1: ID, u2: ID): Funit = (u1 != u2) ?? {
fetchFollows(u1, u2) flatMap {
case true => RelationRepo.unfollow(u1, u2) >>- {
countFollowersCache invalidate u2
countFollowingCache invalidate u1
countFollowersCache.update(u2, _ - 1)
countFollowingCache.update(u1, _ - 1)
reloadOnlineFriends(u1, u2)
lila.mon.relation.unfollow()
}