Skip to content

Commit 75da08d

Browse files
authored
Update net9 (#71)
* feat: Enhance database preparation scripts for PostgreSQL - Added error handling in prepare-database.cmd to check for the presence of the 'psql' command. - Introduced a new Bash script (prepare-database.sh) that provides similar functionality with improved usage instructions and error handling. - Both scripts now support creating and erasing the sample database with clear user prompts. * feat: Add contributing guidelines, security policy, and update README for PostgreSQL caching library - Introduced a new CONTRIBUTING.md file outlining contribution standards and development setup. - Added SECURITY.md to detail security policies, reporting vulnerabilities, and best practices. - Enhanced README.md with a comprehensive introduction, installation instructions, and usage examples. - Updated project to target .NET 6, 8, and 9, and bumped version to 5.0.0. - Improved error handling in DatabaseExpiredItemsRemoverLoop and added logging for better diagnostics. - Updated sample projects to reflect changes in dependencies and configurations.
1 parent 917d98d commit 75da08d

11 files changed

+551
-106
lines changed

CONTRIBUTING.md

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# Contributing to Community.Microsoft.Extensions.Caching.PostgreSql
2+
3+
Thank you for your interest in contributing to our PostgreSQL Distributed Cache implementation for .NET! This document provides guidelines and standards for contributing to the project.
4+
5+
## Development Environment Setup
6+
7+
1. Install the following prerequisites:
8+
9+
- .NET SDK (versions 6.0, 8.0, and 9.0)
10+
- PostgreSQL 11+
11+
- Your preferred IDE (Visual Studio, VS Code, Rider, etc.)
12+
13+
1. Clone the repository:
14+
15+
```bash
16+
git clone https://github.com/leonibr/community-extensions-cache-postgres.git
17+
```
18+
19+
## Code Standards
20+
21+
### Project Structure
22+
23+
- Source code goes in `Extensions.Caching.PostgreSql/`
24+
- Sample projects in `PostgreSqlCacheSample/` and `WebSample/`
25+
- Tests should be placed in a corresponding test project
26+
27+
### Coding Conventions
28+
29+
1. **Language Version**
30+
31+
- Use C# 11 features (`<LangVersion>11</LangVersion>`)
32+
- Target multiple frameworks: net6.0, net8.0, and net9.0
33+
34+
1. **Naming Conventions**
35+
36+
- Use PascalCase for public members and types
37+
- Use camelCase for private fields
38+
- Prefix private fields with underscore (\_)
39+
40+
```csharp
41+
private readonly ILogger<DatabaseOperations> _logger;
42+
```
43+
44+
1. **String Interpolation**
45+
46+
- Use raw string literals for SQL queries
47+
48+
```csharp
49+
public string CreateSchemaAndTableSql =>
50+
$"""
51+
CREATE SCHEMA IF NOT EXISTS "{_schemaName}";
52+
// ...
53+
""";
54+
```
55+
56+
1. **Async/Await**
57+
58+
- Always provide async versions of methods with CancellationToken support
59+
- Use the Async suffix for async methods
60+
61+
```csharp
62+
public async Task DeleteCacheItemAsync(string key, CancellationToken cancellationToken)
63+
```
64+
65+
1. **Dependency Injection**
66+
67+
- Use constructor injection
68+
- Mark dependencies as readonly when possible
69+
70+
```csharp
71+
private readonly ILogger<DatabaseOperations> _logger;
72+
```
73+
74+
1. **Error Handling**
75+
- Use structured logging with ILogger
76+
- Validate input parameters
77+
- Throw appropriate exceptions with meaningful messages
78+
79+
### Documentation
80+
81+
1. **XML Comments**
82+
83+
- Add XML comments for public APIs
84+
- Include parameter descriptions and examples where appropriate
85+
86+
```csharp
87+
/// <summary>
88+
/// The factory to create a NpgsqlDataSource instance.
89+
/// Either <see cref="DataSourceFactory"/> or <see cref="ConnectionString"/> should be set.
90+
/// </summary>
91+
public Func<NpgsqlDataSource> DataSourceFactory { get; set; }
92+
```
93+
94+
1. **README Updates**
95+
- Update README.md when adding new features
96+
- Include usage examples for new functionality
97+
98+
### Testing (optional for now)
99+
100+
1. Write unit tests for new functionality
101+
1. Ensure all tests pass before submitting PR
102+
1. Include integration tests for database operations
103+
104+
## Pull Request Process
105+
106+
1. Create a feature branch from `master`
107+
1. Make your changes following the coding standards
108+
1. Update documentation as needed
109+
1. Run all tests (if any)
110+
1. Submit a pull request with:
111+
- Clear description of changes
112+
- Any related issue numbers
113+
- Breaking changes noted
114+
- Documentation updates
115+
116+
### Dependencies
117+
118+
Keep dependencies up to date with the latest stable versions:
119+
120+
## Release Process
121+
122+
1. Version numbers follow SemVer
123+
1. Update version in .csproj:
124+
125+
```xml
126+
<Version>4.0.0</Version>
127+
```
128+
129+
1. Update PackageReleaseNotes in .csproj
130+
1. Document breaking changes in README.md
131+
132+
## Questions or Problems?
133+
134+
- Open an issue for bugs or feature requests
135+
- For questions, use GitHub Discussions
136+
- For security issues, please see [SECURITY.md](SECURITY.md)
137+
138+
## License
139+
140+
This project is licensed under the MIT License - see the LICENSE file for details.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="16.0">
22
<PropertyGroup>
3-
<TargetFramework>netstandard2.0</TargetFramework>
3+
<TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>
44
<AssemblyName>Community.Microsoft.Extensions.Caching.PostgreSql</AssemblyName>
55
<RootNamespace>Community.Microsoft.Extensions.Caching.PostgreSql</RootNamespace>
66
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
7-
<Version>4.0.0</Version>
7+
<Version>5.0.0</Version>
88
<Authors>Ashley Marques</Authors>
99
<Company />
1010
<Description>DistributedCache using postgres</Description>
@@ -18,17 +18,19 @@
1818
</PropertyGroup>
1919
<ItemGroup>
2020
<None Include="..\README.md" Pack="true" PackagePath="\" />
21+
<None Include="..\SECURITY.md" Pack="true" PackagePath="\" />
22+
<None Include="..\CONTRIBUTING.md" Pack="true" PackagePath="\" />
2123
</ItemGroup>
2224
<!--<ItemGroup>
2325
<Compile Include="**\*.cs" />
2426
<EmbeddedResource Include="**\*.resx" />
2527
</ItemGroup>-->
2628
<ItemGroup>
27-
<PackageReference Include="Dapper" Version="2.0.123" />
28-
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" />
29-
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
30-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
31-
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
32-
<PackageReference Include="Npgsql" Version="7.0.0" />
29+
<PackageReference Include="Dapper" Version="2.1.35" />
30+
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="9.0.0" />
31+
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.0" />
32+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
33+
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.0" />
34+
<PackageReference Include="Npgsql" Version="9.0.2" />
3335
</ItemGroup>
3436
</Project>

Extensions.Caching.PostgreSql/DatabaseExpiredItemsRemoverLoop.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ private async Task DeleteExpiredCacheItems()
9797
_logger.LogDebug($"DeleteExpiredCacheItems was cancelled at {utcNow}");
9898
break;
9999
}
100-
catch (Exception)
100+
catch (Exception ex)
101101
{
102102
//We don't want transient errors from failing next run
103+
_logger.LogError(ex, "An error occurred while deleting expired cache items.");
103104
}
104105
}
105106

Extensions.Caching.PostgreSql/DatabaseOperations.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public DatabaseOperations(IOptions<PostgreSqlCacheOptions> options, ILogger<Data
4040
}
4141

4242
ConnectionFactory = cacheOptions.DataSourceFactory != null
43-
? () => cacheOptions.DataSourceFactory.Invoke().CreateConnection()
43+
? () => cacheOptions.DataSourceFactory.Invoke().CreateConnection()
4444
: new Func<NpgsqlConnection>(() => new NpgsqlConnection(cacheOptions.ConnectionString));
4545

4646
SystemClock = cacheOptions.SystemClock;

PostgreSqlCacheSample/PostgreSqlCacheSample.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
9+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
1010
</ItemGroup>
1111

1212
<ItemGroup>

PostgreSqlCacheSample/Program.cs

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Extensions.Hosting;
33
using Community.Microsoft.Extensions.Caching.PostgreSql;
44
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.Logging;
56

67
namespace PostgreSqlCacheSample
78
{
@@ -14,6 +15,11 @@ public static void Main(string[] args)
1415

1516
private static IHostBuilder CreateHostBuilder(string[] args) =>
1617
Host.CreateDefaultBuilder(args)
18+
.ConfigureLogging(logging=>
19+
{
20+
logging.ClearProviders();
21+
logging.AddConsole();
22+
})
1723
.ConfigureServices((hostContext, services) =>
1824
{
1925
var configuration = hostContext.Configuration;

PostgreSqlCacheSample/prepare-database.cmd

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
@echo off
2+
where psql >nul 2>nul
3+
if %ERRORLEVEL% neq 0 (
4+
echo Error: PostgreSQL command line tool 'psql' not found.
5+
echo Please ensure PostgreSQL Client is installed and added to your PATH.
6+
exit /b 1
7+
)
8+
29
set arg1=%1
310
if "%arg1%"=="-erase" (
411
psql -U postgres -f erase-sample-database.sql
512
)
613
if "%arg1%"=="-create" (
7-
echo 1^. Create database 'cache_test' into localhost
8-
echo using default postgres user
9-
echo 2^. Create a role also called 'cache_test'
10-
echo 3^. Connects to database and create a schema 'name1'
14+
echo 1^. Create database 'cache_test' into localhost
15+
echo using default postgres user
16+
echo 2^. Create a role also called 'cache_test'
17+
echo 3^. Connects to database and create a schema 'name1'
1118
psql -U postgres -f create-sample-database.sql
1219
)
1320
echo Done.
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
show_usage() {
4+
echo "Usage: ./prepare-database.sh [-create|-erase]"
5+
echo " -create Create the sample database and role"
6+
echo " -erase Erase the sample database"
7+
}
8+
9+
if ! command -v psql &> /dev/null; then
10+
echo "Error: psql command not found. Please ensure PostgreSQL is installed and in your PATH."
11+
exit 1
12+
fi
13+
14+
# if no arguments are provided, show usage and exit
15+
if [ $# -eq 0 ]; then
16+
show_usage
17+
exit 1
18+
fi
19+
20+
case "$1" in
21+
"-erase")
22+
echo "Erasing database..."
23+
psql -U postgres -f erase-sample-database.sql
24+
;;
25+
"-create")
26+
echo "1. Create database 'cache_test' into localhost"
27+
echo "2. Create a role also called 'cache_test'"
28+
echo "3. Connects to database and create a schema 'name1'"
29+
psql -U postgres -f create-sample-database.sql
30+
;;
31+
*)
32+
show_usage
33+
exit 1
34+
;;
35+
esac
36+
37+
echo "Done."

0 commit comments

Comments
 (0)