Sample C# code for the Redis for .NET Developers blog series on taswar.zeytinsoft.com. Each folder is a small, self-contained project covering one Redis feature, using the StackExchange.Redis client.
- 📖 Full series: Redis for .NET Developers
- ⭐ Found it useful? Star the repo so other .NET developers can find it.
- 🤖 More C# samples: AI models for C# developers
- 📘 Free ebook: Prompt Engineering for .NET Developers
| # | Topic | Project | Blog post |
|---|---|---|---|
| 1 | Connecting to Redis (.NET Framework and .NET Core) | 1.RedisConnection | Connecting with C# |
| 2 | String data type | 2.RedisString | String data type |
| 3 | Strings as integers | 3.RedisStringsAsInt | String data type, part 2 |
| 4 | Hash data type | 4.RedisHashes | Hash data type |
| 5 | List data type | 5.RedisList | List data type |
| 6 | Set data type | 6.RedisSet | Set data type |
| 7 | Sorted set data type | 7.RedisSortedSets | Sorted set data type |
| 8 | HyperLogLog | 8.RedisHyperLogLog | HyperLogLog |
| 9 | Pub/Sub | 9.RedisPubSub | Pub/Sub |
| 10 | Pipelining and batching | 10.RedisPipeline | Pipeline and batching |
| 11 | Transactions | 11.RedisTransaction | Transactions |
| 12 | Lua scripting | 12.RedisLua | Lua scripting |
| 13 | Geospatial commands (with Ottawa open data) | 13.RedisGeo | GeoSpatial |
| 14 | ASP.NET Core Web API with Redis | 14.RedisAspDotNetCore | Redis with ASP.NET Core Web API |
More in the series, without code samples: Redis running in Docker, Redis password, Redis Sentinel, Redis in Azure and Redis in AWS ElastiCache.
-
A running Redis server. The quickest way is Docker (see Redis running in Docker):
docker run -d --name redis -p 6379:6379 redis
-
Samples 1–13 (.NET Framework): Visual Studio with .NET Framework 4.5.2–4.6.2. NuGet packages restore on build.
-
.NET Core samples (
1.RedisConnection/RedisConnectionDotNetCore,14.RedisAspDotNetCore): the .NET SDK. These targetnetcoreapp2.2, so you may need to changeTargetFrameworkin the.csprojto a version you have installed.
git clone https://github.com/taswar/RedisForNetDevelopers.git
cd RedisForNetDevelopersThe samples were written against Redis running in Docker on different ports, so check the endpoint before you run one:
| Project type | Where to set it | Key |
|---|---|---|
| .NET Framework console apps | App.config |
redis.connection (for example localhost:6379) |
| .NET Core console app | appsettings.json |
redis.connection |
| ASP.NET Core Web API | appsettings.json |
Redis:Hosts |
.NET Framework: open the .sln file in the sample's folder in Visual Studio, set the endpoint in App.config, and press F5.
.NET Core:
cd 1.RedisConnection/RedisConnectionDotNetCore
dotnet runcd 14.RedisAspDotNetCore/RedisAspDotNetCoreWebApiSample
dotnet runThe Web API exposes its endpoints under api/redis; see RedisController.cs.
Fixes and updates are welcome. See CONTRIBUTING.md.
MIT © Taswar Bhatti