Example #1
0
        public byte[] Get(StackExchangeRedisDataTypesEnum type, string key, object extraParams = null)
        {
            var db    = _redis.GetDatabase();
            var value = new RedisValue();

            switch (type)
            {
            case StackExchangeRedisDataTypesEnum.String:
                value = db.StringGet(key);
                break;

            case StackExchangeRedisDataTypesEnum.Hash:
                var castedExtraParams = ParamsCastHelper.TryCastParams <StackExchangeRedisHashParams>(extraParams);
                value = db.HashGet(key, castedExtraParams.HashField);
                break;
            }

            if (!value.IsNull)
            {
                return(value);
            }
            else
            {
                return(null);
            }
        }
Example #2
0
        public bool Delete(StackExchangeRedisDataTypesEnum type, IEnumerable <Tuple <string, object> > keys)
        {
            var db = _redis.GetDatabase();

            switch (type)
            {
            case StackExchangeRedisDataTypesEnum.String:
                return(db.KeyDelete(keys.Select(x => (RedisKey)x.Item1).ToArray()) == keys.Count());

            case StackExchangeRedisDataTypesEnum.Hash:
                if (keys.Select(x => x.Item1).Distinct().Count() > 1)
                {
                    throw new ArgumentException($"Using type {nameof(StackExchangeRedisDataTypesEnum.Hash)} - only one distinct key is allowed");
                }
                var key = keys.First().Item1;

                var hashFields = keys.Select(x =>
                {
                    var castedExtraParams = ParamsCastHelper.TryCastParams <StackExchangeRedisHashParams>(x.Item2);
                    return((RedisValue)castedExtraParams.HashField);
                }).ToArray();

                return(db.HashDelete(key, hashFields) == keys.Count());
            }

            return(false);
        }
Example #3
0
        public bool Delete(StackExchangeRedisDataTypesEnum type, string key, object extraParams)
        {
            var db = _redis.GetDatabase();

            switch (type)
            {
            case StackExchangeRedisDataTypesEnum.String:
                return(db.KeyDelete(key));

            case StackExchangeRedisDataTypesEnum.Hash:
                if (extraParams == null)
                {
                    return(db.KeyDelete(key));
                }
                else
                {
                    var castedExtraParams = ParamsCastHelper.TryCastParams <StackExchangeRedisHashParams>(extraParams);
                    if (!string.IsNullOrEmpty(castedExtraParams.HashField))
                    {
                        return(db.HashDelete(key, castedExtraParams.HashField));
                    }
                    else
                    {
                        return(db.KeyDelete(key));
                    }
                }
            }

            return(false);
        }
Example #4
0
        public bool Set(StackExchangeRedisDataTypesEnum type, IEnumerable <Tuple <string, byte[], object> > values)
        {
            var db = _redis.GetDatabase();

            switch (type)
            {
            case StackExchangeRedisDataTypesEnum.String:
                var stringEntries = values.Select(x => new KeyValuePair <RedisKey, RedisValue>(x.Item1,
                                                                                               x.Item2)).ToArray();

                return(db.StringSet(stringEntries));

            case StackExchangeRedisDataTypesEnum.Hash:
                var keys = values.Select(x => x.Item1).Distinct();

                if (keys.Count() > 1)
                {
                    throw new ArgumentException($"Using type {nameof(StackExchangeRedisDataTypesEnum.Hash)} - only one distinct key is allowed");
                }
                var key = keys.First();

                var hashEntries = values.Select(x =>
                {
                    var castedExtraParams = ParamsCastHelper.TryCastParams <StackExchangeRedisHashParams>(x.Item3);
                    return(new HashEntry(castedExtraParams.HashField, x.Item2));
                }).ToArray();

                db.HashSet(key, hashEntries);

                return(true);
            }

            return(false);
        }
Example #5
0
        public IDictionary <string, byte[]> Get(StackExchangeRedisDataTypesEnum type, IEnumerable <Tuple <string, object> > keys)
        {
            var db = _redis.GetDatabase();

            var res = new Dictionary <string, byte[]>();

            switch (type)
            {
            case StackExchangeRedisDataTypesEnum.String:
                var stringValues = db.StringGet(keys.Select(x => (RedisKey)x.Item1).ToArray());

                var keyList = keys.ToList();
                for (int i = 0; i < keyList.Count; i++)
                {
                    if (!stringValues[i].IsNull)
                    {
                        res[keyList[i].Item1] = stringValues[i];
                    }
                }

                return(res);

            case StackExchangeRedisDataTypesEnum.Hash:
                var distincyKeys = keys.Select(x => x.Item1).Distinct();

                if (distincyKeys.Count() > 1)
                {
                    throw new ArgumentException($"Using type {nameof(StackExchangeRedisDataTypesEnum.Hash)} - only one distinct key is allowed");
                }
                var key = distincyKeys.First();

                var hashFields = keys.Select(x =>
                {
                    var castedExtraParams = ParamsCastHelper.TryCastParams <StackExchangeRedisHashParams>(x.Item2);
                    return((RedisValue)castedExtraParams.HashField);
                }).ToArray();

                var hashValues = db.HashGet(key, hashFields);

                for (int i = 0; i < keys.Count(); i++)
                {
                    if (!hashValues[i].IsNull)
                    {
                        res[hashFields[i]] = hashValues[i];
                    }
                }
                return(res);
            }

            return(null);
        }
Example #6
0
        public bool Exists(StackExchangeRedisDataTypesEnum type, string key, object extraParams)
        {
            var db = _redis.GetDatabase();

            switch (type)
            {
            case StackExchangeRedisDataTypesEnum.String:
                return(db.KeyExists(key));

            case StackExchangeRedisDataTypesEnum.Hash:
                var castedExtraParams = ParamsCastHelper.TryCastParams <StackExchangeRedisHashParams>(extraParams);
                return(db.HashExists(key, castedExtraParams.HashField));
            }

            return(false);
        }
Example #7
0
        public bool Set(StackExchangeRedisDataTypesEnum type, string key, byte[] value, object extraParams = null)
        {
            var db = _redis.GetDatabase();

            switch (type)
            {
            case StackExchangeRedisDataTypesEnum.String:
                return(db.StringSet(key, value));

            case StackExchangeRedisDataTypesEnum.Hash:
                var castedExtraParams = ParamsCastHelper.TryCastParams <StackExchangeRedisHashParams>(extraParams);
                return(db.HashSet(key, castedExtraParams.HashField, value));
            }

            return(false);
        }