Beispiel #1
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="overtimeInterval">超时间隔(单位:秒)</param>
 /// <param name="cleanInterval">清除时间间隔(单位:秒)</param>
 public AgileConcurrentFileMemoryCache(double overtimeInterval, double clearInterval, string filePath, AgileFileType fileType = AgileFileType.Text)
 {
     if (overtimeInterval > 0)
     {
         FilePath         = filePath;
         FileType         = fileType;
         cache            = AgileFile.BinaryRead <ConcurrentDictionary <TKey, TValue> >(FilePath, FileType);
         OvertimeInterval = overtimeInterval;
         timer            = new Timer(clearInterval * 1000);
         timer.Elapsed   += timeElapsed;
         timer.Start();
     }
 }
Beispiel #2
0
        /// <summary>
        /// 获得输出流
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="fileType">文件类型</param>
        /// <param name="fileEncoding">文件编码</param>
        /// <param name="fileMode">文件模式</param>
        /// <param name="fileAccess">文件控制</param>
        /// <returns>结果</returns>
        public static StreamWriter GetStreamWriter(string path, AgileFileType fileType = AgileFileType.Text, Encoding fileEncoding = null, FileMode fileMode = FileMode.Append, FileAccess fileAccess = FileAccess.Write)
        {
            if (fileEncoding == null)
            {
                fileEncoding = Encoding.Default;
            }
            FileStream fileStream = new FileStream(path, fileMode, fileAccess);

            if (fileType == AgileFileType.GZip)
            {
                GZipStream gzipStream = new GZipStream(fileStream, CompressionMode.Compress);

                return(new StreamWriter(gzipStream, fileEncoding));
            }
            else
            {
                return(new StreamWriter(fileStream, fileEncoding));
            }
        }
Beispiel #3
0
        /// <summary>
        /// 获得读取流
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="fileType">文件类型</param>
        /// <param name="fileEncoding">文件编码</param>
        /// <returns>结果</returns>
        public static StreamReader GetStreamReader(string path, AgileFileType fileType = AgileFileType.Text, Encoding fileEncoding = null)
        {
            if (fileEncoding == null)
            {
                fileEncoding = Encoding.Default;
            }
            FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);

            if (fileType == AgileFileType.GZip)
            {
                GZipStream gzipStream = new GZipStream(fileStream, CompressionMode.Decompress);

                return(new StreamReader(gzipStream, fileEncoding));
            }
            else
            {
                return(new StreamReader(fileStream, fileEncoding));
            }
        }
Beispiel #4
0
        /// <summary>
        /// 将对象转成二进制文件存储到对应的目录
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="path">文件地址</param>
        /// <param name="fileType">文件类型</param>
        /// <param name="fileMode">打开文件方式</param>
        /// <param name="fileAccess">文件访问方式</param>
        public static void BinaryWrite(object obj, string path, AgileFileType fileType = AgileFileType.Text, FileMode fileMode = FileMode.OpenOrCreate, FileAccess fileAccess = FileAccess.Write)
        {
            using (FileStream fileStream = new FileStream(path, fileMode, fileAccess))
            {
                BinaryFormatter formatter = new BinaryFormatter();

                if (fileType == AgileFileType.Text)
                {
                    formatter.Serialize(fileStream, obj);
                }
                else
                {
                    using (GZipStream gzipStream = new GZipStream(fileStream, CompressionMode.Compress))
                    {
                        formatter.Serialize(gzipStream, obj);
                    }
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// 将二进制文件转成对象并返回
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="path">文件地址</param>
        /// <param name="fileType">文件类型</param>
        /// <param name="fileMode">打开文件方式</param>
        /// <param name="fileAccess">文件访问方式</param>
        /// <returns>对象</returns>
        public static T BinaryRead <T>(string path, AgileFileType fileType = AgileFileType.Text, FileMode fileMode = FileMode.Open, FileAccess fileAccess = FileAccess.Read)
        {
            if (!File.Exists(path))
            {
                return(default(T));
            }
            BinaryFormatter formatter = new BinaryFormatter();

            using (FileStream fileStream = new FileStream(path, fileMode, fileAccess))
            {
                if (fileType == AgileFileType.Text)
                {
                    return((T)formatter.Deserialize(fileStream));
                }
                else
                {
                    using (GZipStream gzipStream = new GZipStream(fileStream, CompressionMode.Decompress))
                    {
                        return((T)formatter.Deserialize(gzipStream));
                    }
                }
            }
        }