Exemple #1
0
        public virtual string Add <T>(T model) where T : IRedisModel
        {
            using (var Redis = RedisClientManager.GetClient())
            {
                if (model == null)
                {
                    return(null);
                }
                if (string.IsNullOrWhiteSpace(model.Id))
                {
                    model.Id = NextId <T>().ToString();
                }
                if (Get <T>(model.Id) != null)
                {
                    return(null);
                }

                string modelKey = GetKey <T>(model);

                model.ModuleName = CurrentMode;

                Redis.Set <T>(modelKey, model);

                Redis.AddItemToSortedSet(RedisKeyFactory.ListAllKeys <T>(), modelKey, model.CreateDateTime.Ticks);

                Redis.IncrementValue(RedisKeyFactory.ListAllNumKeys <T>());

                Redis.IncrementValue(RedisKeyFactory.NextKey <T>());

                BuildIndex <T>(model);

                return(model.Id);
            }
        }
Exemple #2
0
        public int NextId <T>()
            where T : IRedisModelBase
        {
            using (var Redis = RedisClientManager.GetClient())
            {
                int id = Redis.Get <int>(RedisKeyFactory.NextKey <T>()) + 1;

                while (IsExist <T>(id.ToString()))
                {
                    id++;
                    Redis.IncrementValue(RedisKeyFactory.NextKey <T>());
                }

                return(id);
            }
        }