GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Caching Caching
Demis Bellot edited this page
This page has moved to docs.servicestack.net/cachingAs caching is an essential technology in the development of high-performance web services, Service Stack has a number of different caching options available that each share the same common client interface (ICacheClient) for the following cache providers:
To configure which cache should be used, the particular client has to be registered in the IoC container: container.Register<ICacheClient>(new MemoryCacheClient()); NuGet Package: ServiceStackcontainer.Register<IRedisClientsManager>(c => new PooledRedisClientManager("localhost:6379")); container.Register(c => c.Resolve<IRedisClientsManager>().GetCacheClient()); NuGet Package: ServiceStack.Redis//Register OrmLite Db Factory if not already container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(connString, SqlServerDialect.Provider)); container.RegisterAs<OrmLiteCacheClient, ICacheClient>(); //Create 'CacheEntry' RDBMS table if it doesn't exist already container.Resolve<ICacheClient>().InitSchema(); NuGet Package: ServiceStack.Servercontainer.Register<ICacheClient>( new MemcachedClientCache(new[] { "127.0.0.0" }); //Add Memcached hosts NuGet Package: ServiceStack.Caching.Memcachedvar awsDb = new AmazonDynamoDBClient( AWS_ACCESS_KEY, AWS_SECRET_KEY, RegionEndpoint.USEast1); var cache = new DynamoDbCacheClient(new PocoDynamo(awsDb)); cache.InitSchema(); NuGet Package: ServiceStack.Awscontainer.Register<ICacheClient>( new AzureCacheClient("MyAppCache")); //Add your Azure CacheName if any NuGet Package: ServiceStack.Caching.AzureTo cache a response you simply have to call In your service: public class OrdersService : Service { public object Get(CachedOrders request) { var cacheKey = "unique_key_for_this_request"; return base.Request.ToOptimizedResultUsingCache(base.Cache,cacheKey,()=> { //Delegate is executed if item doesn't exist in cache //Any response DTO returned here will be cached automatically }); } }
var cacheKey = "some_unique_key"; //Cache should be deleted in 1h var expireInTimespan = new TimeSpan(1, 0, 0); return base.Request.ToOptimizedResultUsingCache( base.Cache, cacheKey, expireInTimespan, ...) If now for example an order gets updated and the order was cached before the update, the webservice will still return the same result, because the cache doesn't know that the order has been updated. So there are two options:
So if the order gets updated, you should delete the cache manually: public class CachedOrdersService : Service { public object Put(CachedOrders request) { //The order gets updated... var cacheKey = "some_unique_key_for_order"; return base.Request.RemoveFromCache(base.Cache, cacheKey); } } If now the client calls the webservice to request the order, he'll get the latest version. As it sometimes beneficial to have access to a local in-memory Cache in addition to your registered MemoryCacheClient LocalCache { get; } This doesn't affect any existing functionality that utilizes a cache like Sessions which continue to use
your registered var cacheKey = "unique_key_for_this_request"; return base.Request.ToOptimizedResultUsingCache(LocalCache, cacheKey, () => { //Delegate is executed if item doesn't exist in cache }); If you don't register a The ICacheClientExtended API is used to to provide additional non-core functionality to our most popular Caching providers:
The new API's are added as Extension methods on
With these new API's you can now easily get all active User Sessions using any of the supported Caching providers above with: var sessionPattern = IdUtils.CreateUrn<IAuthSession>(""); //= urn:iauthsession: var sessionKeys = Cache.GetKeysStartingWith(sessionPattern).ToList(); var allSessions = Cache.GetAll<IAuthSession>(sessionKeys); A live demo of the ICacheClient is available in The ServiceStack.Northwind's example project. Here are some requests to cached services: Which are simply existing web services wrapped using ICacheClient that are contained in CachedServices.cs
Clone this wiki locally |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |