Skip to content

Commit

Permalink
Rewrote LastEventCache to be based on a 2D dictionary.
Browse files Browse the repository at this point in the history
CachedConnection has also been adjusted to ensure the LastEventCache is
only used to store results to `meta.last_event` calls.
  • Loading branch information
elliottwilliams committed May 27, 2017
1 parent 2b1b625 commit c7377c9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 62 deletions.
21 changes: 13 additions & 8 deletions Proper/Connection/CachedConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@ import Runes

class CachedConnection<C: ConnectionType>: ConnectionType {
let connection: C
let cache = LastEventCache()
let lastEventCache = LastEventCache()

init(_ connection: C) {
self.connection = connection
}

// Returns a producer which will check the cache before calling the underlying connection.
func call(_ proc: String, with args: WampArgs, kwargs: WampKwargs) -> EventProducer {
let hit = EventProducer { observer, _ in
self.cache.lookup(rpc: proc, args).apply(observer.send)
observer.sendCompleted()
}
let hit = cacheLookup(proc, args, kwargs)
let miss = connection.call(proc, with: args, kwargs: kwargs)
.on(value: { [weak self] in self?.cache.store(event: $0, rpc: proc, args: args) })
return SignalProducer<EventProducer, ProperError>([hit, miss])
.flatten(.concat).take(first: 1)
}
Expand All @@ -34,8 +30,17 @@ class CachedConnection<C: ConnectionType>: ConnectionType {
// terminates.
func subscribe(to topic: String) -> EventProducer {
return connection.subscribe(to: topic)
.on(terminated: { [weak self] in self?.cache.expire(lastEventFrom: topic, on: topic) },
value: { [weak self] in _ = self?.cache.store(event: $0, topic: topic) })
.on(terminated: { [weak self] in self?.lastEventCache.expire(topic: topic) },
value: { [weak self] in _ = self?.lastEventCache.store(event: $0, from: topic) })
}

private func cacheLookup(_ proc: String, _ args: WampArgs, _ kwargs: WampKwargs) -> EventProducer {
return EventProducer { observer, _ in
if proc == "meta.last_event", let topic = args[safe: 0] as? String, let originator = args[safe: 1] as? String {
self.lastEventCache.lastEvent(from: originator, sentIn: topic).apply(observer.send)
}
observer.sendCompleted()
}
}
}

Expand Down
69 changes: 15 additions & 54 deletions Proper/Connection/LastEventCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,35 @@

import Foundation

/// `NSCache`-backed storage for looking up responses to `meta.last_event`.
/// Manages storage, retrieval, and expiration of TopicEvents keyed by the originator and topic.
class LastEventCache: NSObject {
private let cache = NSCache<NSString, Box<TopicEvent>>()
private var cache = [String: [String: TopicEvent]]()
private var voids = [String: DispatchWorkItem]()

/// Create a cache which retains around `numEvents` events (default 100).
init (numEvents: Int = 100) {
cache.totalCostLimit = numEvents
}

/// Look up a cache result for an `meta.last_event` call.
func lookup(rpc proc: String, _ args: WampArgs) -> TopicEvent? {
if let (topic, originator) = LastEventCache.eventParams(proc, args) {
return lastMessage(from: originator, sentIn: topic)
} else {
return nil
}
}

/// Look up the last message sent by `originator` in `topic`.
func lastMessage(from originator: String, sentIn topic: String) -> TopicEvent? {
let event = cache.object(forKey: key(topic, originator) as NSString)
return event?.value
}

/// Store the `event` returned by a call to `rpc`. Returns `true` if stored.
func store(event: TopicEvent, rpc proc: String, args: WampArgs) {
if let (metaTopic, _) = LastEventCache.eventParams(proc, args) {
store(event: event, topic: metaTopic)
}
func lastEvent(from originator: String, sentIn topic: String) -> TopicEvent? {
return cache[topic]?[originator]
}

/// Store `event` in the cache. `event` must have an originator. Returns `true` if stored.
func store(event: TopicEvent, topic: String) {
func store(event: TopicEvent, from topic: String) {
if let originator = event.originator {
let key = self.key(topic, originator)
voids[key]?.cancel()
cache.setObject(Box(event), forKey: key as NSString)
voids[topic]?.cancel()
if var topic = cache[topic] {
topic[originator] = event
} else {
cache[topic] = [originator: event]
}
}
}

/// Request `topic` to be removed from the cache at the end of the main loop.
@available(*, unavailable, message: "Use expire(lastEventFrom:on)")
func void(topic: String) {
return
}

/// Informs the cache that a particular `lastEvent` will no longer be updated and will no longer be guaranteed valid.
func expire(lastEventFrom originator: String, on topic: String) {
let key = self.key(topic, originator)
func expire(topic: String) {
let action = DispatchWorkItem { [weak self] in
self?.cache.removeObject(forKey: key as NSString)
self?.voids[key] = nil
self?.cache[topic] = nil
self?.voids[topic] = nil
}
voids[key] = action
voids[topic] = action
DispatchQueue.main.async(execute: action)
}

private static func eventParams(_ proc: String, _ args: WampArgs) -> (metaTopic: String, originator: String)? {
guard let metaTopic = args[safe: 0] as? String, let originator = args[safe: 1] as? String,
proc == "meta.last_event" else {
return nil
}

return (metaTopic, originator)
}

private func key(_ topic: String, _ originator: String) -> String {
return [topic, originator].joined()
}
}

0 comments on commit c7377c9

Please sign in to comment.