Пример #1
0
        /// <summary>
        /// 清除缓存
        /// </summary>
        public void RemoveCache()
        {
            if (string.IsNullOrEmpty(Key))
            {
                return;
            }
            switch (this.cacheType)
            {
            case CacheType.Cache:
                RunTimeCache.RemoveCache(Key);
                break;

            case CacheType.Memcached:
                MemcachedCache.RemoveCache(this.DatabaseOrPath, Key);     // 未经测试
                break;

            case CacheType.File:
                var filename = Barfoo.Library.IO.PathUtility.GetPhysicalPath(this.DatabaseOrPath);
                FileCache.Remove(filename);
                break;

            case CacheType.Session:
                SessionCache.Remove(Key);
                break;

            case CacheType.Redis:
                if (redis == null)
                {
                    redis = RedisHelp.GetConnection();
                }
                redis.Remove(Key);     // 未经测试
                break;
            }
        }
Пример #2
0
        /// <summary>
        /// 设值
        /// </summary>
        /// <param name="value"></param>
        public void Set(object value)
        {
            if (string.IsNullOrEmpty(Key))
            {
                return;
            }
            switch (this.cacheType)
            {
            case CacheType.Cache:
                RunTimeCache.Set(Key, value, TimeOut, this.timeOutType);
                break;

            case CacheType.Memcached:
                MemcachedCache.Set(this.DatabaseOrPath, Key, value, TimeOut, this.timeOutType);
                break;

            case CacheType.File:
                var filename = Barfoo.Library.IO.PathUtility.GetPhysicalPath(this.DatabaseOrPath);
                FileCache.Set(filename, value);
                break;

            case CacheType.Session:
                SessionCache.Set(Key, value);
                break;

            case CacheType.Redis:
                if (redis == null)
                {
                    redis = RedisHelp.GetConnection();
                }
                redis.Set(Key, value);
                break;
            }
        }
Пример #3
0
        /// <summary>
        /// 获取缓存的值
        /// </summary>
        /// <returns></returns>
        public object Get()
        {
            if (string.IsNullOrEmpty(Key))
            {
                return(null);
            }
            switch (this.cacheType)
            {
            case CacheType.Cache:
                return(RunTimeCache.Get(Key));

            case CacheType.Memcached:
                return(MemcachedCache.Get(this.DatabaseOrPath, Key, timeOutType, TimeOut));

            case CacheType.File:
                var filename = Barfoo.Library.IO.PathUtility.GetPhysicalPath(this.DatabaseOrPath);
                return(FileCache.Get(filename, TimeOut));

            case CacheType.Session:
                return(SessionCache.Get(Key));

            case CacheType.Redis:
                if (redis == null)
                {
                    redis = RedisHelp.GetConnection();
                }
                return(redis.Get <object>(Key));   // 类型会被转成 json 格式的字符串

            default:
                return(null);
            }
        }