MichaCo / CacheManager

CacheManager is an open source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.
http://cachemanager.michaco.net
Apache License 2.0
2.34k stars 457 forks source link

Unable to use CacheManager with Redis config in .net core #256

Closed vanikulkarniAtFIS closed 5 years ago

vanikulkarniAtFIS commented 5 years ago

In .net core ConfigureServices, trying to add instance of cache as singleton See following code snippet:

 ICacheManager<Model> cache = CacheFactory.Build<Model>(settings =>
            {
                settings
                    //.WithMicrosoftMemoryCacheHandle()
                    //.And
                    .WithRedisConfiguration("redis", config =>
                    {
                        config.WithAllowAdmin()
                            .WithDatabase(0)
                            .WithEndpoint("localhost", 6379);
                    })
                    .WithMaxRetries(1000)
                    .WithRetryTimeout(100)
                    .WithRedisCacheHandle("redis", true);
            });
            services.AddSingleton(cache);

Getting the below exception:

System.InvalidOperationException
  HResult=0x80131509
  Message=Cache handle CacheManager.Redis.RedisCacheHandle`1 requires serialization of cached values but no serializer has been configured.
  Source=CacheManager.Core
  StackTrace:
   at CacheManager.Core.BaseCacheManager`1..ctor(String name, ICacheManagerConfiguration configuration)
   at CacheManager.Core.BaseCacheManager`1..ctor(ICacheManagerConfiguration configuration)
   at CacheManager.Core.CacheFactory.Build[TCacheValue](String cacheName, Action`1 settings)
   at CacheManager.Core.CacheFactory.Build[TCacheValue](Action`1 settings)
   at CalulationService.ServiceFactory.ConfigureServices(IServiceCollection services) in C:\Projects\poc-optimist-calc-engine\server\CalculationService\ServiceFactory.cs:line 48
   at FIS.Risk.Core.Services.AspNetCore.AspNetCoreServiceFactory.Microsoft.AspNetCore.Hosting.IStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()

Then added CacheManager.Serialization.Json nuget package and chaged config settings to have "WithJsonSerializer"

                settings
                    //.WithMicrosoftMemoryCacheHandle()
                    //.And
                    .WithRedisConfiguration("redis", config =>
                    {
                        config.WithAllowAdmin()
                            .WithDatabase(0)
                            .WithEndpoint("localhost", 6379);
                    })
                    .WithJsonSerializer()
                    .WithMaxRetries(1000)
                    .WithRetryTimeout(100)
                    .WithRedisCacheHandle("redis", true);

This started giving theConnection to Redis failed error, Redis service is up and running

System.InvalidOperationException
  HResult=0x80131509
  Message=Connection to 'localhost:6379,allowAdmin=True,connectTimeout=5000,ssl=False,abortConnect=False,connectRetry=10,proxy=None' failed.
  Source=CacheManager.Core
  StackTrace:
   at CacheManager.Core.BaseCacheManager`1..ctor(String name, ICacheManagerConfiguration configuration)
   at CacheManager.Core.BaseCacheManager`1..ctor(ICacheManagerConfiguration configuration)
   at CacheManager.Core.CacheFactory.Build[TCacheValue](String cacheName, Action`1 settings)
   at CacheManager.Core.CacheFactory.Build[TCacheValue](Action`1 settings)
   at CalulationService.ServiceFactory.ConfigureServices(IServiceCollection services) in C:\Projects\poc-optimist-calc-engine\server\CalculationService\ServiceFactory.cs:line 48
   at FIS.Risk.Core.Services.AspNetCore.AspNetCoreServiceFactory.Microsoft.AspNetCore.Hosting.IStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
vanikulkarniAtFIS commented 5 years ago

Please point us to sample code where .net core is using CacheManager with Redis. If this is a known bug in CacheManager, do let us know when this will get fixed

MichaCo commented 5 years ago

No there is absolutely no known bug using .NET Core. I'm using that combination every day/in production. Both errors are very obvious issues with your configuration/setup. You figured out the first one with serialization. That's by design, you have to specify a serialization method...

Second one is most likely a invalid connection string. Seems Redis is not running on localhost port 6379?

/closed for now. Will re-open if you can provide a reproducible example

vanikulkarniAtFIS commented 5 years ago

Hi Micha,

Thanks for the quick reply. I fail to understand how should I fix this? Could you give me some link for sample app? Redis is running on localhost port 6379, when I use StackExchange.Redis directly , it works

I was referring to https://github.com/MichaCo/CacheManager/issues/94 for our implementation

MichaCo commented 5 years ago

again, your configuration looks fine. Using that configuration actually works for me if I just run Redis locally. There is currently no known issue using CacheManager+Redis on .NET Core 1.x, 2 or 2.1

I don't know why you refer to #94, that issue has nothing todo with the code you showed. (and please stop spamming, thanks)

vanikulkarniAtFIS commented 5 years ago

It worked, thanks a lot. I was referring to #94 for DI registration

Regards, Vani.

From: MichaC [mailto:notifications@github.com] Sent: 03 October 2018 16:00 To: MichaCo/CacheManager CacheManager@noreply.github.com Cc: Kulkarni, Vani Vani.Kulkarni@fisglobal.com; Author author@noreply.github.com Subject: Re: [MichaCo/CacheManager] Unable to use CacheManager with Redis config in .net core (#256)

again, your configuration looks fine. Using that configuration actually works for me if I just run Redis locally. There is currently no known issue using CacheManager+Redis on .NET Core 1.x, 2 or 2.1

I don't know why you refer to #94https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FMichaCo%2FCacheManager%2Fissues%2F94&data=02%7C01%7Cvani.kulkarni%40fisglobal.com%7C68f15c91623947d0380d08d6291b3ce8%7Ce3ff91d834c84b15a0b418910a6ac575%7C0%7C0%7C636741594291063755&sdata=%2FMK0PDTYL5K9gN4XwH6Zhe7W07DVnmSJk0D1rHXzLic%3D&reserved=0, that issue has nothing todo with the code you showed. (and please stop spamming, thanks)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FMichaCo%2FCacheManager%2Fissues%2F256%23issuecomment-426587329&data=02%7C01%7Cvani.kulkarni%40fisglobal.com%7C68f15c91623947d0380d08d6291b3ce8%7Ce3ff91d834c84b15a0b418910a6ac575%7C0%7C0%7C636741594291063755&sdata=Gm6BDZ20%2FYf9KWPC9wR4c788VKv%2FUN9wBuVGNgAkY48%3D&reserved=0, or mute the threadhttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAluDJ_VfKVRALBizqAAuF64CJupOYqvzks5uhJHDgaJpZM4XFpy2&data=02%7C01%7Cvani.kulkarni%40fisglobal.com%7C68f15c91623947d0380d08d6291b3ce8%7Ce3ff91d834c84b15a0b418910a6ac575%7C0%7C0%7C636741594291063755&sdata=tK3CY66VKer%2FeYzdTeU7hldbyiDAIxIjEhietAkO43Y%3D&reserved=0.

The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you.