Buscar contenidos

martes, 12 de junio de 2018

Azure Redis Cache

ASP.NET Session State Provider for Azure Redis Cache

https://github.com/alex-simonov/RedisAspNetProviders
https://www.captechconsulting.com/blogs/azure-redis-cache
https://docs.microsoft.com/en-us/azure/redis-cache/cache-aspnet-session-state-provider


Work with a distributed cache in ASP.NET Core
https://github.com/thiago-vivas/Articles/tree/master/AzureRedisCache
https://dotnetthoughts.net/configuring-redis-for-aspnet-core-session-store/
https://tahirnaushad.com/2017/08/18/asp-net-core-caching/


Quickstart: Use Azure Redis Cache with a .NET application
https://docs.microsoft.com/en-us/azure/redis-cache/cache-dotnet-how-to-use-azure-redis-cache


NetCore...

{
    "ConnectionStrings": {
        "RedisConnection": "ABC.redis.cache.windows.net:6380,password=ABC,ssl=True,abortConnect=False"
    },
    "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
            "Default": "Warning"
        }
    }

}



  public static class RedisSessionHelper
    {
        //if (RedisCacheHelper.GetDatabase() != null )
        //        return View( true );
        //    else
        //        return View( false );

        public static IConfigurationRoot Configuration { get; set; }

        public static T Get<T>(string cacheKey)
        {
            return Deserialize<T>(GetDatabase().StringGet(cacheKey));
        }

        public static object Get(string cacheKey)
        {
            return Deserialize<object>(GetDatabase().StringGet(cacheKey));
        }

        public static void Set(string cacheKey, object cacheValue)
        {
            GetDatabase().StringSet(cacheKey, Serialize(cacheValue));
        }

        private static byte[] Serialize(object obj)
        {
            if (obj == null)
            {
                return null;
            }
            BinaryFormatter objBinaryFormatter = new BinaryFormatter();
            using (MemoryStream objMemoryStream = new MemoryStream())
            {
                objBinaryFormatter.Serialize(objMemoryStream, obj);
                byte[] objDataAsByte = objMemoryStream.ToArray();
                return objDataAsByte;
            }
        }

        private static T Deserialize<T>(byte[] bytes)
        {
            BinaryFormatter objBinaryFormatter = new BinaryFormatter();
            if (bytes == null)
                return default(T);

            using (MemoryStream objMemoryStream = new MemoryStream(bytes))
            {
                T result = (T)objBinaryFormatter.Deserialize(objMemoryStream);
                return result;
            }
        }

        public static IDatabase GetDatabase()
        {
            var builder = new ConfigurationBuilder()
             .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json");
            Configuration = builder.Build();

            IDatabase databaseReturn = null;
            string connectionString = Configuration["ConnectionStrings:RedisConnection"].ToString();
            var connectionMultiplexer = ConnectionMultiplexer.Connect(connectionString);
            if (connectionMultiplexer.IsConnected)
                databaseReturn = connectionMultiplexer.GetDatabase();

            return databaseReturn;
        }

    }


No hay comentarios:

Publicar un comentario