Skip to content

Commit 635c13a

Browse files
committed
Add separate NeoSmart.Caching.Sqlite.AspNetCore project
Should be installed via nuget to get automatic configuration of SQLitePCL w/ bundle_green.
1 parent 86ebc3e commit 635c13a

File tree

9 files changed

+136
-20
lines changed

9 files changed

+136
-20
lines changed

README.md

100644100755
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SqliteCache for ASP.NET Core
22

33
[SqliteCache](https://neosmart.net/blog/2019/sqlite-cache-for-asp-net-core) is a persistent cache
4-
implementing `IDistributedCache` for ASP.NET Core projects.
4+
implementing `IDistributedCache` for .NET and ASP.NET Core projects.
55

66
SqliteCache uses a locally stored SQLite database file (taking advantage of SQLite's battle-tested
77
safe multi-threaded access features) to replicate persistent caching, allowing developers to mimic
@@ -37,6 +37,16 @@ follows:
3737
Install-Package NeoSmart.Caching.Sqlite
3838
```
3939

40+
If using this in an ASP.NET Core project, install NeoSmart.Caching.Sqlite.AspNetCore to get a
41+
convenient helper method for dependency injection (used below):
42+
43+
```
44+
Install-Package NeoSmart.Caching.Sqlite.AspNetCore
45+
```
46+
47+
If you install `NeoSmart.Caching.Sqlite.AspNetCore` you do not need to manually install
48+
`NeoSmart.Caching.Sqlite` as it it will be installed automatically/transitively.
49+
4050
## Usage
4151

4252
Using SqliteCache is straight-forward, and should be extremely familiar for anyone that's configured
@@ -46,10 +56,10 @@ methods.*
4656

4757
If using SqliteCache in an ASP.NET Core project, the SQLite-backed cache should be added as an
4858
`IDistributedCache` type by adding the following to your `ConfigureServices` method, by default
49-
located in `Startup.cs`:
59+
located in `Startup.cs`, after using the correct namespace `NeoSmart.Caching.Sqlite.AspNetCore`:
5060

5161
```csharp
52-
// using NeoSmart.Caching.Sqlite;
62+
// using NeoSmart.Caching.Sqlite.AspNetCore;
5363
5464
public void ConfigureServices(IServiceCollection services)
5565
{
@@ -85,7 +95,9 @@ public class FooModel(DbContext db, IDistributedCache cache)
8595
}
8696
```
8797

88-
To take advantage of SqliteCache-specific features or functionality that aren't exposed via the `IDistributedCache` façade, you'll need to inject `SqliteCache` into your classes/methods rather than `IDistributedCache`. For example, to globally clear the cache after performing some operation:
98+
To take advantage of SqliteCache-specific features or functionality that aren't exposed via the
99+
`IDistributedCache` façade, you'll need to inject `SqliteCache` into your classes/methods rather than
100+
`IDistributedCache`. For example, to globally clear the cache after performing some operation:
89101

90102
```csharp
91103
// using NeoSmart.Caching.Sqlite;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<AssemblyName>NeoSmart.Caching.Sqlite.AspNetCore</AssemblyName>
5+
<RootNamespace>NeoSmart.Caching.Sqlite.AspNetCore</RootNamespace>
6+
<TargetFramework>netstandard2.1</TargetFramework>
7+
<Nullable>enable</Nullable>
8+
<LangVersion>11</LangVersion>
9+
<Version>7.0.1</Version>
10+
<SignAssembly>true</SignAssembly>
11+
<AssemblyOriginatorKeyFile>../SqliteCache/NeoSmart.Caching.Sqlite.snk</AssemblyOriginatorKeyFile>
12+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
13+
<IncludeSymbols>true</IncludeSymbols>
14+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
15+
<PackageReadmeFile>README.md</PackageReadmeFile>
16+
</PropertyGroup>
17+
18+
<ItemGroup>
19+
<None Include="..\README.md">
20+
<Pack>True</Pack>
21+
<PackagePath>\</PackagePath>
22+
</None>
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\SqliteCache\SqliteCache.csproj" />
27+
<PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.6" />
28+
</ItemGroup>
29+
30+
</Project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using Microsoft.Extensions.Caching.Distributed;
3+
using Microsoft.Extensions.DependencyInjection;
4+
5+
namespace NeoSmart.Caching.Sqlite.AspNetCore
6+
{
7+
public static class SqliteCacheServiceCollectionExtensions
8+
{
9+
/// <summary>
10+
/// Registers <c>SqliteCache</c> as a dependency-injected singleton, available
11+
/// both as <c>IDistributedCache</c> and <c>SqliteCache</c>.
12+
/// </summary>
13+
/// <param name="services"></param>
14+
public static IServiceCollection AddSqliteCache(this IServiceCollection services,
15+
Action<SqliteCacheOptions> setupAction)
16+
{
17+
if (services == null)
18+
{
19+
throw new ArgumentNullException(nameof(services));
20+
}
21+
22+
if (setupAction == null)
23+
{
24+
throw new ArgumentNullException(nameof(setupAction));
25+
}
26+
27+
SQLitePCL.Batteries_V2.Init();
28+
services.AddOptions();
29+
services.AddSingleton<SqliteCache>();
30+
services.AddSingleton<IDistributedCache, SqliteCache>(services => services.GetRequiredService<SqliteCache>());
31+
services.Configure(setupAction);
32+
return services;
33+
}
34+
35+
/// <summary>
36+
/// Registers <c>SqliteCache</c> as a dependency-injected singleton, available
37+
/// both as <c>IDistributedCache</c> and <c>SqliteCache</c>.
38+
/// </summary>
39+
/// <param name="services"></param>
40+
/// <param name="path">The path where the SQLite database should be stored. It
41+
/// is created if it does not exist. (The path should be a file path, not a
42+
/// directory. Make sure the application has RW access at runtime.)</param>
43+
/// <returns></returns>
44+
public static IServiceCollection AddSqliteCache(this IServiceCollection services,
45+
string path)
46+
{
47+
return AddSqliteCache(services, options => options.CachePath = path);
48+
}
49+
}
50+
}

SqliteCache.Tests/DependencyInjection.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
#if NETCOREAPP1_0_OR_GREATER
12
using Microsoft.Extensions.Caching.Distributed;
23
using Microsoft.Extensions.DependencyInjection;
34
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using NeoSmart.Caching.Sqlite.AspNetCore;
46
using System;
57

68
namespace NeoSmart.Caching.Sqlite.Tests
@@ -45,3 +47,4 @@ public void TestInjectionSameness()
4547
}
4648
}
4749
}
50+
#endif

SqliteCache.Tests/SqliteCache.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@
2626
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" PrivateAssets="All" Version="1.0.3" />
2727
</ItemGroup>
2828

29+
<ItemGroup Condition="'$(TargetFramework)' != 'net462'">
30+
<ProjectReference Include="..\SqliteCache.AspNetCore\SqliteCache.AspNetCore.csproj" />
31+
</ItemGroup>
32+
2933
</Project>

SqliteCache.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1313
README.md = README.md
1414
EndProjectSection
1515
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqliteCache.AspNetCore", "SqliteCache.AspNetCore\SqliteCache.AspNetCore.csproj", "{BD7FAF15-DB65-4782-BE73-7024DF8C4068}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
2729
{1B6F63DC-522C-4FD1-BF5D-B853163A803E}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{1B6F63DC-522C-4FD1-BF5D-B853163A803E}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{1B6F63DC-522C-4FD1-BF5D-B853163A803E}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{BD7FAF15-DB65-4782-BE73-7024DF8C4068}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{BD7FAF15-DB65-4782-BE73-7024DF8C4068}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{BD7FAF15-DB65-4782-BE73-7024DF8C4068}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{BD7FAF15-DB65-4782-BE73-7024DF8C4068}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

SqliteCache/NullLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public bool IsEnabled(LogLevel logLevel)
2121
return false;
2222
}
2323

24-
public IDisposable BeginScope<TState>(TState state)
24+
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
2525
{
2626
return new NullDisposable();
2727
}

SqliteCache/SqliteCache.csproj

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<LangVersion>9.0</LangVersion>
66
<AssemblyName>NeoSmart.Caching.Sqlite</AssemblyName>
77
<RootNamespace>NeoSmart.Caching.Sqlite</RootNamespace>
8-
<Version>7.0.0</Version>
8+
<Version>7.0.1</Version>
99
<Nullable>enable</Nullable>
1010
<Authors>Mahmoud Al-Qudsi, neosmart, mqudsi</Authors>
1111
<Company>NeoSmart Technologies</Company>
@@ -21,10 +21,12 @@
2121
Version 7.0:
2222
- SqliteCache no longer depends on SQLitePCLRaw.bundle_green.
2323

24-
ASP.NET users do not have to change anything, but other .NET Core users will need to install either
25-
SQLitePCLRaw.bundle_green and call `SQLitePCL.Batteries.Init()` before instantiating SqliteCache,
26-
or else install the correct SQLitePCLRaw.provider.xxx version that matches the target platform and call
27-
`SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_whatever())` before using SqliteCache.
24+
ASP.NET users should install companion package NeoSmart.Caching.Sqlite.AspNetCore.
25+
Other .NET Core users will need to install either SQLitePCLRaw.bundle_green and call
26+
`SQLitePCL.Batteries.Init()` before instantiating SqliteCache, or else install the
27+
correct SQLitePCLRaw.provider.xxx version that matches the target platform and call
28+
`SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_whatever())` before using
29+
SqliteCache.
2830

2931
Version 6.1:
3032
- Add methods to clear the cache (SqliteCache.Clear() and SqliteCache.ClearAsync())
@@ -52,12 +54,9 @@ Version 3.1:
5254
<SignAssembly>true</SignAssembly>
5355
<AssemblyOriginatorKeyFile>NeoSmart.Caching.Sqlite.snk</AssemblyOriginatorKeyFile>
5456
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
55-
</PropertyGroup>
56-
57-
<PropertyGroup>
58-
<IncludeSymbols>true</IncludeSymbols>
59-
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
60-
<PackageReadmeFile>README.md</PackageReadmeFile>
57+
<IncludeSymbols>true</IncludeSymbols>
58+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
59+
<PackageReadmeFile>README.md</PackageReadmeFile>
6160
</PropertyGroup>
6261

6362
<ItemGroup>
@@ -72,7 +71,6 @@ Version 3.1:
7271
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" />
7372
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
7473
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
75-
<!--<PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.4" />-->
7674
<PackageReference Include="SQLitePCLRaw.core" Version="2.1.6" />
7775
</ItemGroup>
7876

SqliteCache/SqliteCacheServiceCollectionExtensions.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@ namespace NeoSmart.Caching.Sqlite
66
{
77
public static class SqliteCacheServiceCollectionExtensions
88
{
9+
/// <summary>
10+
/// Registers <c>SqliteCache</c> as a dependency-injected singleton, available
11+
/// both as <c>IDistributedCache</c> and <c>SqliteCache</c>.
12+
/// <br /><br/>
13+
/// If you're using ASP.NET Core, install <c>NeoSmart.Caching.Sqlite.AspNetCore</c>
14+
/// and add <c>using namespace NeoSmart.Caching.Sqlite.AspNetCore</c> to get a version
15+
/// of this method that does not require the <c>sqlite3Provider</c> parameter.
16+
/// </summary>
17+
/// <param name="services"></param>
918
public static IServiceCollection AddSqliteCache(this IServiceCollection services,
10-
Action<SqliteCacheOptions> setupAction)
19+
Action<SqliteCacheOptions> setupAction, SQLitePCL.ISQLite3Provider sqlite3Provider)
1120
{
1221
if (services == null)
1322
{
@@ -29,16 +38,20 @@ public static IServiceCollection AddSqliteCache(this IServiceCollection services
2938
/// <summary>
3039
/// Registers <c>SqliteCache</c> as a dependency-injected singleton, available
3140
/// both as <c>IDistributedCache</c> and <c>SqliteCache</c>.
41+
/// <br /><br/>
42+
/// If you're using ASP.NET Core, install <c>NeoSmart.Caching.Sqlite.AspNetCore</c>
43+
/// and add <c>using namespace NeoSmart.Caching.Sqlite.AspNetCore</c> to get a version
44+
/// of this method that does not require the <c>sqlite3Provider</c> parameter.
3245
/// </summary>
3346
/// <param name="services"></param>
3447
/// <param name="path">The path where the SQLite database should be stored. It
3548
/// is created if it does not exist. (The path should be a file path, not a
3649
/// directory. Make sure the application has RW access at runtime.)</param>
3750
/// <returns></returns>
3851
public static IServiceCollection AddSqliteCache(this IServiceCollection services,
39-
string path)
52+
string path, SQLitePCL.ISQLite3Provider sqlite3Provider)
4053
{
41-
return AddSqliteCache(services, options => options.CachePath = path);
54+
return AddSqliteCache(services, options => options.CachePath = path, sqlite3Provider);
4255
}
4356
}
4457
}

0 commit comments

Comments
 (0)