-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
9 changed files
with
101 additions
and
102 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
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,56 @@ | ||
namespace Equinox.Core | ||
|
||
open System | ||
|
||
type CacheItemOptions = | ||
| AbsoluteExpiration of DateTimeOffset | ||
| RelativeExpiration of TimeSpan | ||
|
||
[<AllowNullLiteral>] | ||
type CacheEntry<'state>(initialToken: StreamToken, initialState: 'state, supersedes: StreamToken -> StreamToken -> bool) = | ||
let mutable currentToken, currentState = initialToken, initialState | ||
member __.UpdateIfNewer(other: CacheEntry<'state>) = | ||
lock __ <| fun () -> | ||
let otherToken, otherState = other.Value | ||
if otherToken |> supersedes currentToken then | ||
currentToken <- otherToken | ||
currentState <- otherState | ||
member __.Value: StreamToken * 'state = | ||
lock __ <| fun () -> | ||
currentToken, currentState | ||
|
||
|
||
type ICache = | ||
abstract member UpdateIfNewer: cacheItemOptions:CacheItemOptions -> key: string -> CacheEntry<'state> -> Async<unit> | ||
abstract member TryGet: key: string -> Async<(StreamToken * 'state) option> | ||
|
||
namespace Equinox | ||
|
||
open System.Runtime.Caching | ||
open Equinox.Core | ||
|
||
type Cache(name, sizeMb : int) = | ||
let cache = | ||
let config = System.Collections.Specialized.NameValueCollection(1) | ||
config.Add("cacheMemoryLimitMegabytes", string sizeMb); | ||
new MemoryCache(name, config) | ||
|
||
let getPolicy (cacheItemOption: CacheItemOptions) = | ||
match cacheItemOption with | ||
| AbsoluteExpiration absolute -> new CacheItemPolicy(AbsoluteExpiration = absolute) | ||
| RelativeExpiration relative -> new CacheItemPolicy(SlidingExpiration = relative) | ||
|
||
interface ICache with | ||
member this.UpdateIfNewer cacheItemOptions key entry = async { | ||
let policy = getPolicy cacheItemOptions | ||
match cache.AddOrGetExisting(key, box entry, policy) with | ||
| null -> () | ||
| :? CacheEntry<'state> as existingEntry -> existingEntry.UpdateIfNewer entry | ||
| x -> failwithf "UpdateIfNewer Incompatible cache entry %A" x } | ||
|
||
member this.TryGet key = async { | ||
return | ||
match cache.Get key with | ||
| null -> None | ||
| :? CacheEntry<'state> as existingEntry -> Some existingEntry.Value | ||
| x -> failwithf "TryGet Incompatible cache entry %A" x } |
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
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
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