Fix db access

- Add NuGet properties
- Update RethinkDB F# driver
This commit is contained in:
Daniel J. Summers 2022-04-19 14:05:48 -04:00
parent 21ef9bac02
commit 26d7c9b140
3 changed files with 43 additions and 38 deletions

View File

@ -52,19 +52,19 @@ type DistributedRethinkDBCache (options : IOptions<DistributedRethinkDBCacheOpti
let db = defaultArg (Option.ofObj opts.Database) "" let db = defaultArg (Option.ofObj opts.Database) ""
/// The table name; default to "Cache" if not provided /// The table name; default to "Cache" if not provided
let table = match defaultArg (Option.ofObj opts.TableName) "" with "" -> "Cache" | tbl -> tbl let tbl = match defaultArg (Option.ofObj opts.TableName) "" with "" -> "Cache" | tbl -> tbl
/// The name of the cache /// The name of the cache
let cacheName = let table =
seq { seq {
match db with "" -> () | _ -> $"{db}." match db with "" -> () | _ -> $"{db}."
table tbl
} }
|> Seq.reduce (+) |> Seq.reduce (+)
/// Debug message /// Debug message
let dbug text = 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 /// Make sure the RethinkDB database, table, expiration index exist
let checkEnvironment (_ : CancellationToken) = let checkEnvironment (_ : CancellationToken) =
@ -84,23 +84,23 @@ type DistributedRethinkDBCache (options : IOptions<DistributedRethinkDBCacheOpti
do! rethink { dbCreate db; write; withRetryDefault; ignoreResult opts.Connection } do! rethink { dbCreate db; write; withRetryDefault; ignoreResult opts.Connection }
dbug <| fun () -> " ...done" dbug <| fun () -> " ...done"
// Table // Table
dbug <| fun () -> sprintf $" Checking for table {table} existence..." dbug <| fun () -> sprintf $" Checking for table {tbl} existence..."
let! tables = rethink<string list> { tableList db; result; withRetryDefault opts.Connection } let! tables = rethink<string list> { tableList db; result; withRetryDefault opts.Connection }
if not (tables |> List.contains table) then if not (tables |> List.contains tbl) then
dbug <| fun () -> sprintf $" ...creating table {table}..." dbug <| fun () -> sprintf $" ...creating table {tbl}..."
do! rethink { withDb db; tableCreate table; write; withRetryDefault; ignoreResult opts.Connection } do! rethink { tableCreate table; write; withRetryDefault; ignoreResult opts.Connection }
dbug <| fun () -> " ...done" dbug <| fun () -> " ...done"
// Index // Index
dbug <| fun () -> sprintf $" Checking for index {table}.expiresAt..." dbug <| fun () -> sprintf $" Checking for index {tbl}.expiresAt..."
let! indexes = rethink<string list> { let! indexes = rethink<string list> {
withDb db; withTable table withTable table
indexList indexList
result; withRetryDefault opts.Connection result; withRetryDefault opts.Connection
} }
if not (indexes |> List.contains "expiresAt") then 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 { do! rethink {
withDb db; withTable table withTable table
indexCreate "expiresAt" indexCreate "expiresAt"
write; withRetryDefault; ignoreResult opts.Connection write; withRetryDefault; ignoreResult opts.Connection
} }
@ -115,7 +115,7 @@ type DistributedRethinkDBCache (options : IOptions<DistributedRethinkDBCacheOpti
let tix = DateTime.UtcNow.Ticks - 1L let tix = DateTime.UtcNow.Ticks - 1L
dbug <| fun () -> $"Purging expired entries (<= %i{tix})" dbug <| fun () -> $"Purging expired entries (<= %i{tix})"
do! rethink { do! rethink {
withDb db; withTable table withTable table
between (r.Minval ()) tix [ BetweenOptArg.Index "expiresAt" ] between (r.Minval ()) tix [ BetweenOptArg.Index "expiresAt" ]
delete delete
write; withRetryDefault; ignoreResult opts.Connection write; withRetryDefault; ignoreResult opts.Connection
@ -128,7 +128,7 @@ type DistributedRethinkDBCache (options : IOptions<DistributedRethinkDBCacheOpti
/// Get the cache entry specified /// Get the cache entry specified
let getCacheEntry (key : string) (_ : CancellationToken) = let getCacheEntry (key : string) (_ : CancellationToken) =
rethink<CacheEntry> { rethink<CacheEntry> {
withDb db; withTable table withTable table
get key get key
resultOption; withRetryDefault opts.Connection resultOption; withRetryDefault opts.Connection
} }
@ -138,7 +138,7 @@ type DistributedRethinkDBCache (options : IOptions<DistributedRethinkDBCacheOpti
backgroundTask { backgroundTask {
if entry.slidingExpiration > 0 then if entry.slidingExpiration > 0 then
do! rethink { do! rethink {
withDb db; withTable table withTable table
get entry.id get entry.id
update [ "expiresAt", ticksFromNow entry.slidingExpiration :> obj ] update [ "expiresAt", ticksFromNow entry.slidingExpiration :> obj ]
write; withRetryDefault; ignoreResult opts.Connection write; withRetryDefault; ignoreResult opts.Connection
@ -177,7 +177,7 @@ type DistributedRethinkDBCache (options : IOptions<DistributedRethinkDBCacheOpti
cnxToken.ThrowIfCancellationRequested () cnxToken.ThrowIfCancellationRequested ()
do! checkEnvironment cnxToken do! checkEnvironment cnxToken
do! rethink { do! rethink {
withDb db; withTable table withTable table
get key get key
delete delete
write; withRetryDefault; ignoreResult opts.Connection write; withRetryDefault; ignoreResult opts.Connection
@ -208,21 +208,14 @@ type DistributedRethinkDBCache (options : IOptions<DistributedRethinkDBCacheOpti
slidingExpiration = 0 slidingExpiration = 0
} }
|> addExpiration |> addExpiration
match! getCacheEntry key cnxToken with
| None ->
do! rethink { do! rethink {
withDb db; withTable table withTable table
insert entry
write; withRetryDefault; ignoreResult opts.Connection
}
| Some _ ->
do! rethink {
withDb db; withTable table
get key
replace entry replace entry
write; withRetryDefault; ignoreResult opts.Connection write; withRetryDefault; ignoreResult opts.Connection
} }
} }
/// Execute a task synchronously
let runSync (task : CancellationToken -> Task<'T>) = let runSync (task : CancellationToken -> Task<'T>) =
task CancellationToken.None |> (Async.AwaitTask >> Async.RunSynchronously) task CancellationToken.None |> (Async.AwaitTask >> Async.RunSynchronously)

View File

@ -10,10 +10,10 @@ open System
type IServiceCollection with type IServiceCollection with
member this.AddDistributedRethinkDBCache (options : Action<DistributedRethinkDBCacheOptions>) = member this.AddDistributedRethinkDBCache (options : Action<DistributedRethinkDBCacheOptions>) =
match options with null -> nullArg "options" | _ -> () if isNull options then nullArg "options"
ignore <| this.AddOptions () this.AddOptions () |> ignore
ignore <| this.Configure options this.Configure options |> ignore
ignore <| this.Add (ServiceDescriptor.Transient<IDistributedCache, DistributedRethinkDBCache>()) this.Add (ServiceDescriptor.Transient<IDistributedCache, DistributedRethinkDBCache> ())
this this
/// <summary> /// <summary>

View File

@ -3,13 +3,25 @@
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks> <TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
<VersionPrefix>0.9.0</VersionPrefix>
<VersionSuffix>alpha02</VersionSuffix>
<Authors>danieljsummers</Authors>
<PackageProjectUrl>https://github.com/danieljsummers/RethinkDB.DistributedCache</PackageProjectUrl>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<RepositoryUrl>https://github.com/danieljsummers/RethinkDB.DistributedCache</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<Copyright>MIT License</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>RethinkDB IDistributedCache ASP.NET Core</PackageTags>
<Description>An IDistributedCache implementation utilizing RethinkDB for storage</Description>
<PackageReleaseNotes>Updated to .NET 6</PackageReleaseNotes>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="*" /> <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="*" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
<PackageReference Include="RethinkDb.Driver.FSharp" Version="0.8.0-alpha-0002" /> <PackageReference Include="RethinkDb.Driver.FSharp" Version="0.8.0-alpha-0003" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>