Domanda

I has a class named xxxxxx.Bussiness,it not inheritance ServiceStack.ServiceInterface.

but i want to use cache,how do it?

È stato utile?

Soluzione

If you want to access the cache outside of a Service (i.e. a class that inherits from ServiceStack.ServiceInterface) then you can use the IoC resolve method to get the instance of the cache.

Configure Cache Provider:

First you need to register your cache provider with the IoC in the Configure method of your AppHost. ServiceStack supports a number of providers. (In-memory, Redis, OrmLite, Memcached, Azure). See here for details on how to configure the required provider. The example below uses in-memory caching.

public override void Configure(Funq.Container container)
{
    // Choose the Cache Provider you want to use

    // i.e. Register in Memory Cache Client
    container.Register<ICacheClient>(new MemoryCacheClient());
}

Resolve the ICacheClient:

Then when you want to use the cache outside of the Service, then you should try resolve the ICacheClient using the HostContext:

var cache = HostContext.TryResolve<ICacheClient>();
if(cache != null)
{
    // Use the cache object
    cache.Add<string>("Key","Value");
    var value = cache.Get<string>("Key");
}

ICacheClient provided methods:

This will provide you with access to the cache. ICacheClient defines these methods (Original commented interface definition here):

public interface ICacheClient : IDisposable
{
    bool Remove(string key);
    void RemoveAll(IEnumerable<string> keys);
    T Get<T>(string key);
    long Increment(string key, uint amount);
    long Decrement(string key, uint amount);
    bool Add<T>(string key, T value);
    bool Set<T>(string key, T value);
    bool Replace<T>(string key, T value);
    bool Add<T>(string key, T value, DateTime expiresAt);
    bool Set<T>(string key, T value, DateTime expiresAt);
    bool Replace<T>(string key, T value, DateTime expiresAt);
    bool Add<T>(string key, T value, TimeSpan expiresIn);
    bool Set<T>(string key, T value, TimeSpan expiresIn);
    bool Replace<T>(string key, T value, TimeSpan expiresIn);
    void FlushAll();
    IDictionary<string, T> GetAll<T>(IEnumerable<string> keys);
    void SetAll<T>(IDictionary<string, T> values);
}

Cast as your Cache Provider Client:

You can also cast the client to the specific type of your provider, for it's full functionality. For example if we were using the Redis Cache:

var cache = HostContext.TryResolve<ICacheClient>() as RedisClient; // Typed
if(cache != null)
{
    var mySet = cache.GetAllItemsFromSet("MySetKey"); // Redis specific cache method
}

I hope this helps.

Altri suggerimenti

You have to:

  1. register the CacheClient in AppHost

    public override void Configure(Funq.Container container)
    {
        //...
    
        container.Register<ICacheClient>(new MemoryCacheClient());
    
        //...
    }          
    
  2. In your service method use Request.ToOptimizedResultUsingCache

    public object Get(Task request)
    {
        return Request.ToOptimizedResultUsingCache(Cache,
            UrnId.Create<Task>("Id", request.Id),
            () => new TaskResponse
            {
                Task = TaskLogicService.GetTask(request.Id),
                ResponseStatus = new ResponseStatus()
            });
    } 
    

There is an article on ServiceStack that explains Cache usage at greater level of details.

NB: The above snippet asumes use of SS v4.0

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top