Exemplo n.º 1
0
        private void Add(string key, object value, int millisecondsToExpire)
        {
            object valueToAdd = value?.ToString() ?? string.Empty;
            var    fullKey    = GenerateFullKey(key);

            if (value != null && value.GetType().IsArray)
            {
                try
                {
                    var array = (object[, ])value;

                    var table = new MyTable(array);
                    valueToAdd = JsonConvert.SerializeObject(table);
                }
                catch (Exception ex)
                {
                    if (ex.Message.IndexOf("cast object", StringComparison.InvariantCultureIgnoreCase) > 0) //most likely the array is not bi-dimensional, try again with only 1 dimenion
                    {
                        var array = (object[])value;

                        var table = new MyTable(array);
                        valueToAdd = JsonConvert.SerializeObject(table);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            if (millisecondsToExpire > 0)
            {
                CacheFactory.GetInstance().StringSet(fullKey, (string)valueToAdd, TimeSpan.FromMilliseconds(millisecondsToExpire));
            }
            else
            {
                CacheFactory.GetInstance().StringSet(fullKey, (string)valueToAdd);
            }
        }
Exemplo n.º 2
0
        public void Set(string key, object value, int SecondsToExpire)
        {
            object valueToAdd = value?.ToString() ?? string.Empty;

            if (value != null && value.GetType().IsArray)
            {
                try
                {
                    var array = (object[, ])value;
                    var table = new MyTable(array);
                    valueToAdd = JsonConvert.SerializeObject(table);
                }
                catch (Exception ex)
                {
                    if (ex.Message.IndexOf("cast object", StringComparison.InvariantCultureIgnoreCase) > 0) //most likely the array is not bi-dimensional, try again with only 1 dimenion
                    {
                        var array = (object[])value;
                        var table = new MyTable(array);
                        valueToAdd = JsonConvert.SerializeObject(table);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            if (SecondsToExpire > 0)
            {
                _redisinstance.StringSet(key, (string)valueToAdd, TimeSpan.FromSeconds(SecondsToExpire));
            }
            else
            {
                _redisinstance.StringSet(key, (string)valueToAdd);
            }
        }