Page 1 of 8

Rest API and Caching

PostPosted: Fri Aug 03, 2018 12:39 pm
by SBarnes
Hi Mike,

Have you looked at caching any of the Web API services as suggested under the following to improve efficiency?

http://docs.servicestack.net/caching

Re: Rest API and Caching

PostPosted: Fri Aug 03, 2018 5:17 pm
by Mike.Sheen
SBarnes wrote:Hi Mike,

Have you looked at caching any of the Web API services as suggested under the following to improve efficiency?

http://docs.servicestack.net/caching


Hi Stuart,

Yes - we just haven't implemented built-in caching support yet. We realise that people will want choice as to which caching provider to use, so we just need to bake in some options / settings for that and change all our services in the REST API plugin to leverage the ToOptimizedResultUsingCache method.

We'll also need a way of invalidating cache entries automatically when the Jiwa business logic updates tables directly - we'll need to inform the REST API that a urnid is invalidated or even better if we could update the cache from within our business logic.

It'll be a few weeks before we can sneak something like this in - not entirely sure if we can get it into a service release for 7.2 or if it'll require a data structure change in which case maybe 7.2.1.

Mike

Re: Rest API and Caching

PostPosted: Fri Aug 03, 2018 5:33 pm
by SBarnes
Hi Mike,

I tried to get it to work with Redis locally today in a Web API extension plugin with the following code in the configure call

Code: Select all
         Container.Register<IRedisClientsManager>(c => new RedisManagerPool("localhost:6379"));

         Container.Register(c => c.Resolve<IRedisClientsManager>().GetCacheClient());


But even the Swagger UI won't work and you get this as an error

Code: Select all
500 : {"__type":"ServiceStack.ErrorResponse, ServiceStack.Interfaces","ResponseStatus":{"ErrorCode":"TypeInitializationException","Message":"The type initializer for 'ServiceStack.Text.Json.JsonWriter`1' threw an exception.","StackTrace":" at ServiceStack.Text.Json.JsonWriter`1.WriteFn()\r\n at ServiceStack.Text.Json.JsonWriter.GetWriteFn(Type type)\r\n at ServiceStack.Text.JsonSerializer.SerializeToString(Object value, Type type)\r\n at ServiceStack.Text.JsonSerializer.SerializeToString[T](T value)\r\n at ServiceStack.StringExtensions.ToJson[T](T obj)\r\n at ServiceStack.Redis.RedisClient.ToBytes[T](T value)\r\n at ServiceStack.Redis.RedisClient.<>c__DisplayClass149_0`1.<Set>b__0(RedisClient r)\r\n at ServiceStack.Redis.RedisClient.Exec(Action`1 action)\r\n at ServiceStack.Redis.RedisClient.Set[T](String key, T value, TimeSpan expiresIn)\r\n at ServiceStack.Redis.RedisClientManagerCacheClient.Set[T](String key, T value, TimeSpan expiresIn)\r\n at ServiceStack.ServiceExtensions.CacheSet[T](ICacheClient cache, String key, T value, Nullable`1 expiresIn)\r\n at ServiceStack.ServiceStackHost.OnSaveSession(IRequest httpReq, IAuthSession session, Nullable`1 expiresIn)\r\n at ServiceStack.ServiceExtensions.SaveSession(IRequest httpReq, IAuthSession session, Nullable`1 expiresIn)\r\n at JiwaFinancials.Jiwa.JiwaServiceModel.RESTAPIPlugin.<>c__DisplayClass13.<Configure>b__c(IRequest req, IResponse res, Object responseDto) in c:\\ProgramData\\Jiwa Financials\\Jiwa 7\\7.1.0\\SYSTEM\\localhost\\XXXXXXXXXX\\Plugins\\Admin\\Compile\\REST API\\REST API.cs:line 184\r\n at ServiceStack.ServiceStackHost.ApplyResponseFiltersSingle(IRequest req, IResponse res, Object response)\r\n at ServiceStack.ServiceStackHost.ApplyResponseFilters(IRequest req, IResponse res, Object response)\r\n at ServiceStack.Host.RestHandler.<>c__DisplayClass13_1.<ProcessRequestAsync>b__1(Object response)\r\n at ServiceStack.Host.Handlers.ServiceStackHandlerBase.HandleResponse(Object response, Func`2 callback)"}} http://localhost:81/openapi


Do you have any idea what could be wrong, the only thought I am having on it at present is could it be being caused by our little Serializable and Redis issue?

Re: Rest API and Caching

PostPosted: Sat Aug 04, 2018 11:36 am
by SBarnes
Further to this the in memory cache with the following code in the the configure function works

Code: Select all
Container.Register<ICacheClient>(new MemoryCacheClient());


I can now also confirm it is a problem with the Jiwa API somehow as a completely separate ServiceStack project with Redis Caching works on the same machine that is producing the error against the same instance of Redis.

Re: Rest API and Caching

PostPosted: Sun Aug 05, 2018 3:47 pm
by SBarnes
Hi Mike,

To do with this error I created a new demo database and tried turning on Redis Caching and got the same error straight from the Web API Plugin, I can now tell you what is causing it blow up its Global Response Filter that sets the time out for the session if I comment the code below out the swagger ui comes up without the error.

Code: Select all
               AppHost.GlobalResponseFilters.Add((req, res, responseDto) => {
                   var session = req.GetSession();
                  if (session is JiwaAuthUserSession)
                  {
                     JiwaAuthUserSession jiwaAuthUserSession = (JiwaAuthUserSession)session;
                     jiwaAuthUserSession.ConcurrentRequestCount -= (jiwaAuthUserSession.ConcurrentRequestCount > 0) ? 1 : 0;                     
                  }
                  
                     if (session != null)
                         req.SaveSession(session, TimeSpan.FromMinutes(sessionExpiryInMinutes));      
               });

Re: Rest API and Caching

PostPosted: Wed Aug 15, 2018 7:32 pm
by Mike.Sheen
Hi Stuart,

I've not had a chance to investigate this yet, but I have something which might be worth trying - as it would be quite quick and easy for you to try out.

The JiwaAuthUserSession class has a property - Manager :

Code: Select all
public JiwaApplication.Manager Manager { get; set; }


We use that to associate a session with a Manager instance - I'm thinking the caching provider is trying to serialise the JiwaAuthUserSession and it can't because of the Manager property which is not serialisable.

So, my suggestion is to mark the Manager property as not serialisable - by adding the XmlIgnore attribute to that property:

Code: Select all
[System.Xml.Serialization.XmlIgnore]
public JiwaApplication.Manager Manager { get; set; }


Mike

Re: Rest API and Caching

PostPosted: Wed Aug 15, 2018 9:27 pm
by SBarnes
Hi Mike,

That doesn't fix the problem, the Swagger UI still produces the same error.

I've upgraded to 7.2 but I can confirm the line that produces the exception is

Code: Select all
req.SaveSession(session, TimeSpan.FromMinutes(sessionExpiryInMinutes)); 

Re: Rest API and Caching

PostPosted: Thu Aug 16, 2018 4:57 pm
by Mike.Sheen
Ok, what about
Code: Select all
[System.Runtime.Serialization.IgnoreDataMember]
?

Re: Rest API and Caching

PostPosted: Thu Aug 16, 2018 6:56 pm
by SBarnes
Hi Mike,

That stops the error under the Swagger UI but it is going to cause other issues, as even getting a list of warehouses now blows up for example with null references.

Clearly its going to come down to annotating most things with Serialiszable that cause an issue I suppose.

Thanks.

Re: Rest API and Caching

PostPosted: Sat Aug 25, 2018 9:29 am
by SBarnes
Hi Mike,

Is there any way of solving this without a major version release?