-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving Caching logic to central file (#318)
- Loading branch information
Showing
5 changed files
with
69 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module Equinox.Core.Caching | ||
|
||
type internal Decorator<'event, 'state, 'context>(inner : ICategory<'event, 'state, string, 'context>, updateCache : string -> StreamToken * 'state -> Async<unit>) = | ||
|
||
let cache streamName inner = async { | ||
let! tokenAndState = inner | ||
do! updateCache streamName tokenAndState | ||
return tokenAndState } | ||
|
||
interface ICategory<'event, 'state, string, 'context> with | ||
member _.Load(log, streamName : string, opt) : Async<StreamToken * 'state> = | ||
inner.Load(log, streamName, opt) |> cache streamName | ||
|
||
member _.TrySync(log : Serilog.ILogger, streamName, streamToken, state, events : 'event list, context) : Async<SyncResult<'state>> = async { | ||
match! inner.TrySync(log, streamName, streamToken, state, events, context) with | ||
| SyncResult.Conflict resync -> return SyncResult.Conflict (resync |> cache streamName) | ||
| SyncResult.Written (token', state') -> | ||
do! updateCache streamName (token', state') | ||
return SyncResult.Written (token', state') } | ||
|
||
let applyCacheUpdatesWithSlidingExpiration | ||
(cache : ICache) | ||
(prefix : string) | ||
(slidingExpiration : System.TimeSpan) | ||
(category : ICategory<'event, 'state, string, 'context>) | ||
supersedes | ||
: ICategory<'event, 'state, string, 'context> = | ||
let mkCacheEntry (initialToken : StreamToken, initialState : 'state) = new CacheEntry<'state>(initialToken, initialState, supersedes) | ||
let options = CacheItemOptions.RelativeExpiration slidingExpiration | ||
let addOrUpdateSlidingExpirationCacheEntry streamName value = cache.UpdateIfNewer(prefix + streamName, options, mkCacheEntry value) | ||
Decorator<'event, 'state, 'context>(category, addOrUpdateSlidingExpirationCacheEntry) :> _ | ||
|
||
let applyCacheUpdatesWithFixedTimeSpan | ||
(cache : ICache) | ||
(prefix : string) | ||
(lifetime : System.TimeSpan) | ||
(category : ICategory<'event, 'state, string, 'context>) | ||
supersedes | ||
: ICategory<'event, 'state, string, 'context> = | ||
let mkCacheEntry (initialToken : StreamToken, initialState : 'state) = CacheEntry<'state>(initialToken, initialState, supersedes) | ||
let addOrUpdateFixedLifetimeCacheEntry streamName value = | ||
let expirationPoint = let creationDate = System.DateTimeOffset.UtcNow in creationDate.Add lifetime | ||
let options = CacheItemOptions.AbsoluteExpiration expirationPoint | ||
cache.UpdateIfNewer(prefix + streamName, options, mkCacheEntry value) | ||
Decorator<'event, 'state, 'context>(category, addOrUpdateFixedLifetimeCacheEntry) :> _ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.