Skip to content

Commit 1ba503f

Browse files
committed
Update caching.md
1 parent a1df264 commit 1ba503f

File tree

1 file changed

+48
-46
lines changed

1 file changed

+48
-46
lines changed

MyApp/_pages/caching.md

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ As caching is an essential technology in the development of high-performance web
77
[common client interface (ICacheClient)](https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Interfaces/Caching/ICacheClient.cs)
88
for the following cache providers:
99

10-
* [Memory Cache](https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/Caching/MemoryCacheClient.cs) - Useful for single host web services without needing any infrastructure dependencies.
11-
* [Redis](https://github.com/ServiceStack/ServiceStack.Redis) - A fast key-value store with non-volatile persistent storage and support for rich comp-sci data structures.
12-
* [OrmLiteCacheClient](https://www.nuget.org/packages/ServiceStack.Server) - Supports all [OrmLite's RDBMS providers](https://github.com/ServiceStack/ServiceStack.OrmLite/#download) for using an existing RDBMS as a distributed cache.
13-
* [Memcached](https://nuget.org/packages/ServiceStack.Caching.Memcached) - The original, tried and tested distributed memory caching provider.
14-
* [Aws DynamoDB](https://www.nuget.org/packages/ServiceStack.Aws/) - Uses Amazon's Dynamo DB backend hosted on Amazon Web Services
15-
* [Azure Table Storage](/azure#virtual-filesystem-backed-by-azure-blob-storage) - Uses Azure Table Storage for when your application is hosted on Azure.
10+
* [Memory Cache](https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/Caching/MemoryCacheClient.cs) - Useful for single host web services without needing any infrastructure dependencies.
11+
* [Redis](https://github.com/ServiceStack/ServiceStack.Redis) - A fast key-value store with non-volatile persistent storage and support for rich comp-sci data structures.
12+
* [OrmLiteCacheClient](https://www.nuget.org/packages/ServiceStack.Server) - Supports all [OrmLite's RDBMS providers](https://github.com/ServiceStack/ServiceStack.OrmLite/#download) for using an existing RDBMS as a distributed cache.
13+
* [Memcached](https://nuget.org/packages/ServiceStack.Caching.Memcached) - The original, tried and tested distributed memory caching provider.
14+
* [Aws DynamoDB](https://www.nuget.org/packages/ServiceStack.Aws/) - Uses Amazon's Dynamo DB backend hosted on Amazon Web Services
15+
* [Azure Table Storage](/azure#virtual-filesystem-backed-by-azure-blob-storage) - Uses Azure Table Storage for when your application is hosted on Azure.
1616

1717
### Async Cache Clients
1818

@@ -74,55 +74,57 @@ container.Register(c => c.Resolve<IRedisClientsManager>().GetCacheClient());
7474

7575
### OrmLite
7676

77-
```csharp
77+
```csharp
7878
//Register OrmLite Db Factory if not already
79-
container.Register<IDbConnectionFactory>(c =>
80-
new OrmLiteConnectionFactory(connString, SqlServerDialect.Provider));
79+
services.AddSingleton<IDbConnectionFactory>(c =>
80+
new OrmLiteConnectionFactory(connString, SqlServerDialect.Provider));
8181

82-
container.RegisterAs<OrmLiteCacheClient, ICacheClient>();
82+
services.AddSingleton<ICacheClient, OrmLiteCacheClient>();
8383

8484
//Create 'CacheEntry' RDBMS table if it doesn't exist already
85-
container.Resolve<ICacheClient>().InitSchema();
86-
```
85+
appHost.Resolve<ICacheClient>().InitSchema();
86+
```
8787

8888
#### SQL Server Memory Optimized Cache
8989

9090
SQL Server's Memory Optimized support can be used to improve the performance of `OrmLiteCacheClient`
9191
by configuring it to use the above In Memory Table Schema instead, e.g:
9292

9393
```csharp
94-
container.Register<ICacheClient>(c =>
94+
services.AddSingleton<ICacheClient>(c =>
9595
new OrmLiteCacheClient<SqlServerMemoryOptimizedCacheEntry>());
9696
```
9797

9898
##### NuGet Package: [ServiceStack.Server](http://www.nuget.org/packages/ServiceStack.Server)
9999

100-
### Memcached:
101-
```csharp
102-
container.Register<ICacheClient>(
100+
### Memcached
101+
102+
```csharp
103+
services.AddSingleton<ICacheClient>(
103104
new MemcachedClientCache(new[] { "127.0.0.0" }); //Add Memcached hosts
104105
```
105106

106107
##### NuGet Package: [ServiceStack.Caching.Memcached](http://www.nuget.org/packages/ServiceStack.Caching.Memcached)
107108
108-
### AWS DynamoDB:
109+
### AWS DynamoDB
109110

110111
```csharp
111112
var awsDb = new AmazonDynamoDBClient(
112113
AWS_ACCESS_KEY, AWS_SECRET_KEY, RegionEndpoint.USEast1);
113114

114-
container.Register<IPocoDynamo>(new PocoDynamo(awsDb));
115-
container.Register<ICacheClient>(c => new DynamoDbCacheClient(c.Resolve<IPocoDynamo>()));
115+
services.AddSingleton<IPocoDynamo>(new PocoDynamo(awsDb));
116+
services.AddSingleton<ICacheClient>(c => new DynamoDbCacheClient(c.Resolve<IPocoDynamo>()));
116117

117-
var cache = container.Resolve<ICacheClient>();
118+
var cache = appHost.Resolve<ICacheClient>();
118119
cache.InitSchema();
119120
```
121+
120122
##### NuGet Package: [ServiceStack.Aws](http://www.nuget.org/packages/ServiceStack.Aws)
121123
122124
### Azure:
123125

124-
```csharp
125-
container.Register<ICacheClient>(new AzureTableCacheClient(cacheConnStr));
126+
```csharp
127+
services.AddSingleton<ICacheClient>(new AzureTableCacheClient(cacheConnStr));
126128
```
127129

128130
##### NuGet Package: [ServiceStack.Azure](http://www.nuget.org/packages/ServiceStack.Azure)
@@ -134,7 +136,7 @@ registered cache providers whilst "reads" are only accessed until a value exists
134136
and redis server backed Cache Client with:
135137

136138
```csharp
137-
container.Register<ICacheClient>(c => new MultiCacheClient(
139+
services.AddSingleton<ICacheClient>(c => new MultiCacheClient(
138140
new MemoryCacheClient(),
139141
c.Resolve<IRedisClientsManager>().GetCacheClient()));
140142
```
@@ -169,9 +171,9 @@ There exists a class named [UrnId](https://github.com/ServiceStack/ServiceStack/
169171
```csharp
170172
var cacheKey = "some_unique_key";
171173
//Cache should be deleted in 1h
172-
var expireInTimespan = new TimeSpan(1, 0, 0);
174+
var expireInTimeSpan = new TimeSpan(1, 0, 0);
173175
return base.Request.ToOptimizedResultUsingCache(
174-
base.Cache, cacheKey, expireInTimespan, ...)
176+
base.Cache, cacheKey, expireInTimeSpan, ...)
175177
```
176178

177179
## Delete cached responses
@@ -180,8 +182,8 @@ If now for example an order gets updated and the order was cached before the upd
180182

181183
So there are two options:
182184

183-
- Use **time based** caching (and expire cache earlier)
184-
- Cache on **validity**
185+
* Use **time based** caching (and expire cache earlier)
186+
* Cache on **validity**
185187

186188
::: info
187189
When the cache is based on **validity** the caches are invalidated manually (e.g. when a user modified his profile, > clear his cache) which means you always get the latest version and you never need to hit the database again to rehydrate the cache if it hasn't changed, which will save resources
@@ -201,12 +203,12 @@ public class CachedOrdersService : Service
201203
}
202204
```
203205

204-
If now the client calls the webservice to request the order, he'll get the latest version.
206+
If now the client calls the web service to request the order, he'll get the latest version.
205207

206-
### LocalCache
208+
### LocalCache
207209

208-
As it sometimes beneficial to have access to a local in-memory Cache in addition to your registered `ICacheClient`
209-
[Caching Provider](/caching) we also pre-register a `MemoryCacheClient` that all your Services now have access to from the `LocalCache`
210+
As it sometimes beneficial to have access to a local in-memory Cache in addition to your registered `ICacheClient`
211+
[Caching Provider](/caching) we also pre-register a `MemoryCacheClient` that all your Services now have access to from the `LocalCache`
210212
property, i.e:
211213

212214
```csharp
@@ -241,17 +243,17 @@ The [ICacheClientExtended](https://github.com/ServiceStack/ServiceStack/blob/mas
241243
API is used to to provide additional non-core functionality to our most popular
242244
[Caching providers](/caching):
243245

244-
- Redis
245-
- OrmLite RDBMS
246-
- In Memory
247-
- AWS
248-
- Azure
249-
250-
The new API's are added as Extension methods on `ICacheClient` so they're easily accessible without casting, the new API's available include:
246+
* Redis
247+
* OrmLite RDBMS
248+
* In Memory
249+
* AWS
250+
* Azure
251+
252+
The new API's are added as Extension methods on `ICacheClient` so they're easily accessible without casting, the new API's available include:
251253

252-
- GetKeysByPattern(pattern) - return keys matching a wildcard pattern
253-
- GetAllKeys() - return all keys in the caching provider
254-
- GetKeysStartingWith() - Streaming API to return all keys Starting with a prefix
254+
* GetKeysByPattern(pattern) - return keys matching a wildcard pattern
255+
* GetAllKeys() - return all keys in the caching provider
256+
* GetKeysStartingWith() - Streaming API to return all keys Starting with a prefix
255257

256258
With these new API's you can now easily get all active User Sessions using any of the supported Caching providers above with:
257259

@@ -267,16 +269,16 @@ var allSessions = Cache.GetAll<IAuthSession>(sessionKeys);
267269
The `CacheClientWithPrefix` class lets you decorate any `ICacheClient` to prefix all cache keys using the `.WithPrefix()` extension method. This could be used to easily enable multi-tenant usage of a single redis instance, e.g:
268270

269271
```csharp
270-
container.Register(c =>
271-
c.Resolve<IRedisClientsManager>().GetCacheClient().WithPrefix("site1"));
272+
services.AddSingleton(c =>
273+
c.GetRequiredService<IRedisClientsManager>().GetCacheClient().WithPrefix("site1"));
272274
```
273275

274276
## Live Example and code
275277

276278
A live demo of the ICacheClient is available in [The ServiceStack.Northwind's example project](https://northwind.netcore.io/). Here are some requests to cached services:
277279
278-
* [/customers](https://northwind.netcore.io/cached/customers)
279-
* [/customers/ALFKI](https://northwind.netcore.io/cached/customers/ALFKI)
280-
* [/customers/ALFKI/orders](https://northwind.netcore.io/cached/customers/ALFKI/orders)
280+
* [/customers](https://northwind.netcore.io/cached/customers)
281+
* [/customers/ALFKI](https://northwind.netcore.io/cached/customers/ALFKI)
282+
* [/customers/ALFKI/orders](https://northwind.netcore.io/cached/customers/ALFKI/orders)
281283
282284
Which are simply existing web services wrapped using **ICacheClient** that are contained in [CachedServices.cs](https://github.com/ServiceStack/ServiceStack.Examples/blob/master/src/ServiceStack.Northwind/ServiceStack.Northwind.ServiceInterface/CachedServices.cs)

0 commit comments

Comments
 (0)