WIP on expired sessions

This commit is contained in:
Daniel J. Summers 2022-04-21 22:44:17 -04:00
parent c79306078c
commit b87f3fbead

View File

@ -79,7 +79,6 @@ module Entry =
open System.Text open System.Text
open Microsoft.Extensions.Caching.Distributed open Microsoft.Extensions.Caching.Distributed
open RethinkDb.Driver.Ast
open RethinkDb.Driver.Model open RethinkDb.Driver.Model
/// RethinkDB /// RethinkDB
@ -106,28 +105,30 @@ module Entry =
let get cacheOpts (key : string) (cancelToken : CancellationToken) = backgroundTask { let get cacheOpts (key : string) (cancelToken : CancellationToken) = backgroundTask {
let table = table cacheOpts let table = table cacheOpts
let now = ticksFromNow 0 let now = ticksFromNow 0
let filters : (ReqlExpr -> obj) list = [
fun row -> row.G(expiresAt).Gt now
fun row -> row.G(slidingExp).Gt 0
fun row -> row.G(absoluteExp).Gt(0).Or(row.G(absoluteExp).Ne(row.G expiresAt))
]
let expiration (row : ReqlExpr) : obj =
r.HashMap(
expiresAt,
r.Branch(row.G(expiresAt).Add(row.G(slidingExp)).Gt(row.G(absoluteExp)), row.G(absoluteExp),
row.G(slidingExp).Add(now)))
let! result = rethink<Result> { let! result = rethink<Result> {
withTable table withTable table
get key get key
filter filters update (fun row ->
update expiration [ ReturnChanges All ] r.HashMap(
expiresAt,
r.Branch(
// If we have neither sliding nor absolute expiration, do not change the expiry time
row.G(slidingExp).Le(0).Or(row.G(absoluteExp).Le(0)).Or(row.G(absoluteExp).Eq(row.G(expiresAt))),
row.G(expiresAt),
// If the sliding expiry increment exceeds the absolute expiry, use the absolute
row.G(expiresAt).Add(row.G(slidingExp)).Gt(row.G(absoluteExp)),
row.G(absoluteExp),
// Else adjust for the sliding expiry increment
row.G(slidingExp).Add(now))) :> obj) [ ReturnChanges All ]
write cancelToken; withRetryDefault cacheOpts.Connection write cancelToken; withRetryDefault cacheOpts.Connection
} }
match result.Changes.Count with match result.Changes.Count with
| 0 -> return None | 0 -> return None
| _ -> | _ ->
match result.ChangesAs<CacheEntry>().[0].NewValue with match (box >> Option.ofObj) (result.ChangesAs<CacheEntry>().[0].NewValue) with
| entry when entry.expiresAt > now -> return Some entry | Some boxedEntry ->
let entry = unbox boxedEntry
return if entry.expiresAt > now then Some entry else None
| _ -> return None | _ -> return None
} }