From 26d7c9b140cca07a32a735bda0a2c8cafb88dd99 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Tue, 19 Apr 2022 14:05:48 -0400 Subject: [PATCH] Fix db access - Add NuGet properties - Update RethinkDB F# driver --- .../DistributedRethinkDBCache.fs | 53 ++++++++----------- .../IServiceCollectionExtensions.fs | 14 ++--- .../RethinkDB.DistributedCache.fsproj | 14 ++++- 3 files changed, 43 insertions(+), 38 deletions(-) diff --git a/src/RethinkDB.DistributedCache/DistributedRethinkDBCache.fs b/src/RethinkDB.DistributedCache/DistributedRethinkDBCache.fs index f1b37ae..d31b14c 100644 --- a/src/RethinkDB.DistributedCache/DistributedRethinkDBCache.fs +++ b/src/RethinkDB.DistributedCache/DistributedRethinkDBCache.fs @@ -52,19 +52,19 @@ type DistributedRethinkDBCache (options : IOptions "Cache" | tbl -> tbl + let tbl = match defaultArg (Option.ofObj opts.TableName) "" with "" -> "Cache" | tbl -> tbl /// The name of the cache - let cacheName = + let table = seq { match db with "" -> () | _ -> $"{db}." - table + tbl } |> Seq.reduce (+) /// Debug message let dbug text = - if log.IsEnabled LogLevel.Debug then log.LogDebug $"[{cacheName}] %s{text ()}" + if log.IsEnabled LogLevel.Debug then log.LogDebug $"[{table}] %s{text ()}" /// Make sure the RethinkDB database, table, expiration index exist let checkEnvironment (_ : CancellationToken) = @@ -84,23 +84,23 @@ type DistributedRethinkDBCache (options : IOptions " ...done" // Table - dbug <| fun () -> sprintf $" Checking for table {table} existence..." + dbug <| fun () -> sprintf $" Checking for table {tbl} existence..." let! tables = rethink { tableList db; result; withRetryDefault opts.Connection } - if not (tables |> List.contains table) then - dbug <| fun () -> sprintf $" ...creating table {table}..." - do! rethink { withDb db; tableCreate table; write; withRetryDefault; ignoreResult opts.Connection } + if not (tables |> List.contains tbl) then + dbug <| fun () -> sprintf $" ...creating table {tbl}..." + do! rethink { tableCreate table; write; withRetryDefault; ignoreResult opts.Connection } dbug <| fun () -> " ...done" // Index - dbug <| fun () -> sprintf $" Checking for index {table}.expiresAt..." + dbug <| fun () -> sprintf $" Checking for index {tbl}.expiresAt..." let! indexes = rethink { - withDb db; withTable table + withTable table indexList result; withRetryDefault opts.Connection } if not (indexes |> List.contains "expiresAt") then - dbug <| fun () -> sprintf $" ...creating index expiresAt on table {table}..." + dbug <| fun () -> sprintf $" ...creating index expiresAt on table {tbl}..." do! rethink { - withDb db; withTable table + withTable table indexCreate "expiresAt" write; withRetryDefault; ignoreResult opts.Connection } @@ -115,7 +115,7 @@ type DistributedRethinkDBCache (options : IOptions $"Purging expired entries (<= %i{tix})" do! rethink { - withDb db; withTable table + withTable table between (r.Minval ()) tix [ BetweenOptArg.Index "expiresAt" ] delete write; withRetryDefault; ignoreResult opts.Connection @@ -128,7 +128,7 @@ type DistributedRethinkDBCache (options : IOptions { - withDb db; withTable table + withTable table get key resultOption; withRetryDefault opts.Connection } @@ -138,7 +138,7 @@ type DistributedRethinkDBCache (options : IOptions 0 then do! rethink { - withDb db; withTable table + withTable table get entry.id update [ "expiresAt", ticksFromNow entry.slidingExpiration :> obj ] write; withRetryDefault; ignoreResult opts.Connection @@ -177,7 +177,7 @@ type DistributedRethinkDBCache (options : IOptions addExpiration - match! getCacheEntry key cnxToken with - | None -> - do! rethink { - withDb db; withTable table - insert entry - write; withRetryDefault; ignoreResult opts.Connection - } - | Some _ -> - do! rethink { - withDb db; withTable table - get key - replace entry - write; withRetryDefault; ignoreResult opts.Connection - } + do! rethink { + withTable table + replace entry + write; withRetryDefault; ignoreResult opts.Connection + } } + + /// Execute a task synchronously let runSync (task : CancellationToken -> Task<'T>) = task CancellationToken.None |> (Async.AwaitTask >> Async.RunSynchronously) diff --git a/src/RethinkDB.DistributedCache/IServiceCollectionExtensions.fs b/src/RethinkDB.DistributedCache/IServiceCollectionExtensions.fs index 2cc350e..46d8ede 100644 --- a/src/RethinkDB.DistributedCache/IServiceCollectionExtensions.fs +++ b/src/RethinkDB.DistributedCache/IServiceCollectionExtensions.fs @@ -9,12 +9,12 @@ open System type IServiceCollection with - member this.AddDistributedRethinkDBCache(options : Action) = - match options with null -> nullArg "options" | _ -> () - ignore <| this.AddOptions () - ignore <| this.Configure options - ignore <| this.Add (ServiceDescriptor.Transient()) - this + member this.AddDistributedRethinkDBCache (options : Action) = + if isNull options then nullArg "options" + this.AddOptions () |> ignore + this.Configure options |> ignore + this.Add (ServiceDescriptor.Transient ()) + this /// /// Add RethinkDB options to the services collection @@ -23,4 +23,4 @@ type IServiceCollection with /// The given for further manipulation [] let AddDistributedRethinkDBCache (this : IServiceCollection, options : Action) = - this.AddDistributedRethinkDBCache options \ No newline at end of file + this.AddDistributedRethinkDBCache options \ No newline at end of file diff --git a/src/RethinkDB.DistributedCache/RethinkDB.DistributedCache.fsproj b/src/RethinkDB.DistributedCache/RethinkDB.DistributedCache.fsproj index d67bc54..15fed42 100644 --- a/src/RethinkDB.DistributedCache/RethinkDB.DistributedCache.fsproj +++ b/src/RethinkDB.DistributedCache/RethinkDB.DistributedCache.fsproj @@ -3,13 +3,25 @@ net6.0;netstandard2.0 true + 0.9.0 + alpha02 + danieljsummers + https://github.com/danieljsummers/RethinkDB.DistributedCache + false + https://github.com/danieljsummers/RethinkDB.DistributedCache + Git + MIT License + MIT + RethinkDB IDistributedCache ASP.NET Core + An IDistributedCache implementation utilizing RethinkDB for storage + Updated to .NET 6 - +