Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PersonalCacheStruct<GameUser> RemoveCache can not be success #15

Open
Jesse1205 opened this issue Jun 22, 2016 · 2 comments
Open

PersonalCacheStruct<GameUser> RemoveCache can not be success #15

Jesse1205 opened this issue Jun 22, 2016 · 2 comments

Comments

@Jesse1205
Copy link

Jesse1205 commented Jun 22, 2016

fail:
var cache = new PersonalCacheStruct();
var user = cache.FindKey("138021"); //user has value
cache.RemoveCache(user); // return true
var temp = cache.FindKey("138021"); //return null
success:
var cache = new PersonalCacheStruct();
var user = cache.FindKey("138021"); //user has value
cache.RemoveCache(user); // return true
cache.RemoveCache(user); // return false
var temp = cache.FindKey("138021"); //temp has value
so , RemoveCache() can not work well !

@kybird
Copy link

kybird commented Jun 22, 2016

EntityContainer.cs

change below function

        public bool TryRemove(string groupKey, string key, Func<T, bool> callback)
        {

            CacheItemSet itemSet;
            if (Container.TryGetValue(groupKey, out itemSet))
            {
                T entityData;
                var items = (BaseCollection)itemSet.GetItem();
                if (items.TryRemove(key, out entityData))
                {
                    entityData.IsInCache = false;
                    entityData.IsExpired = true;
                    if (callback != null && callback(entityData))
                    {
                        //Not trigger event notify
                        entityData.Dispose();
                    }

                    // BUG
                    //return true;
                }
                if (items.Count == 0 && Container.TryRemove(groupKey, out itemSet))
                {
                    itemSet.ResetStatus();
                    itemSet.Dispose();
                }
            }
            return false;
        }

@kybird
Copy link

kybird commented Feb 14, 2017

My solution makes bad effect. Do not apply it.

reproduce step

  1. remark "return true" in TryRemove function in EntityContainer.cs
           var cacheSet = new PersonalCacheStruct<DBSaveTestEntity>();

            var entity = new DBSaveTestEntity()
            {
                UserID = 9999991,
                UserName = "99999991",
            };

            cacheSet.Add(new DBSaveTestEntity() { UserID = 9999991, UserName = "9999991" });
            cacheSet.Add(new DBSaveTestEntity() { UserID = 9999992, UserName = "9999992" });
            cacheSet.Add(new DBSaveTestEntity() { UserID = 9999993, UserName = "9999993" });

            

            var entity1 = cacheSet.FindKey("9999991");
            var entity2 = cacheSet.FindKey("9999992");
            var entity3 = cacheSet.FindKey("9999993");

            Assert.AreEqual(entity1.UserID , "9999991".ToLong());
            Assert.AreEqual(entity2.UserID , "9999992".ToLong());
            Assert.AreEqual(entity3.UserID ,"9999993".ToLong());


            // reproduce problem
            cacheSet.RemoveCache(entity1);

            var deleted_entity = cacheSet.FindKey("9999991");
            // deleted_entity will have data.
            // because cacheSet think this is first time. it load data from redis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants