Beispiel #1
0
        public void Load(int userId)
        {
            //load from mysql
            List <Mail> mails;

            using (IDbConnection connection = GameManager.DbFactory.OpenDbConnection())
            {
                mails = connection.Select <Mail>(m => m.UserId == userId);
            }
            //add to cache
            var mailDict = new ConcurrentDictionary <int, object>();

            foreach (var mail in mails)
            {
                mailDict.TryAdd(mail.SourceId, mail);
            }
            mailCache.Add(userId.ToString(), mailDict);
            //add to redis
            string key = typeof(Mail).FullName;

            List <HashEntry> hashEntries = new List <HashEntry>();

            foreach (var mail in mails)
            {
                string field = mail.UserId.ToString() + "_" + mail.SourceId.ToString();
                byte[] value = ProtoBufUtils.Serialize(mail);
                hashEntries.Add(new HashEntry(field, value));
            }
            var redisDb = GameManager.RedisMultiplexer.GetDatabase(0);

            redisDb.HashSet(key, hashEntries.ToArray());
            //这种结构情况下,怎样从redis加载到内存???
        }
Beispiel #2
0
        public void Load(int userId)
        {
            if (userCache[userId.ToString()] != null)
            {
                return;
            }
            //load from mysql
            User user;

            using (IDbConnection connection = GameManager.DbFactory.OpenDbConnection())
            {
                user = connection.SingleById <User>(userId);
            }
            //add to cache
            userCache.Add(userId.ToString(), user);
            //add to redis
            var redisDb = GameManager.RedisMultiplexer.GetDatabase(0);
            var key     = user.GetType().FullName;
            var field   = userId;
            var value   = ProtoBufUtils.Serialize(user);

            redisDb.HashSet(key, field, value);
        }
Beispiel #3
0
 public void Load(int userId)
 {
     if (petCache[userId.ToString()] != null)
     {
         return;
     }
     using (IDbConnection connection = GameManager.DbFactory.OpenDbConnection())
     {
         var pets    = connection.Select <Pet>(m => m.UserId == userId);
         var petDict = new ConcurrentDictionary <long, object>();
         foreach (var pet in pets)
         {
             petDict.TryAdd(pet.UniqueId, pet);
         }
         petCache.Add(userId.ToString(), petDict);
     }
 }