示例#1
0
        public string Get(string key)
        {
            //从Redis取数据
            var result = redisHelper.Get(key);

            if (result == null)
            {
                try
                {
                    //获取锁
                    if (Monitor.TryEnter(LockObj))
                    {
                        //从数据库取数据
                        result = "Select * From DbTable";
                        if (result != null)
                        {
                            //写入Redis缓存
                            redisHelper.Insert(key, result);
                        }
                        //释放锁
                        Monitor.Exit(LockObj);
                    }
                    else
                    {
                        Thread.Sleep(100);
                        result = redisHelper.Get(key);
                    }
                }
                finally
                {
                    if (Monitor.IsEntered(LockObj))
                    {
                        Monitor.Exit(LockObj);
                    }
                }
            }
            return(result.ToString());
        }