/// <summary> /// 对不同类型的文件进行数据库操作 /// </summary> /// <param name="type">类型</param> private string OperationDB(Type type, CacheModel cacheModel) { //int count = 0; //DataSet ds = GetDatas(""); //table = ds.Tables[0]; //// 判断是否存在该Key,如果存在,则删除目录下之前的文件,增加现在Key所对应的文件;不存在,直接新增文件 //if (table.Rows.Count < 1) //{ // cacheModel.LatestPutTime = DateTime.Now; // Insert(type, cacheModel); //} //else //{ // foreach (DataRow row in table.Rows) // { // if (cacheModel.Key == row["Key"].ToString()) // { // try // { // if (type == Type.File || type == Type.Obj) // { // File.Delete(this.dir + "\\" + row["Value"].ToString()); // } // else // { // Directory.Delete(this.dir + "\\" + row["Value"].ToString(), true); // int index = row["Value"].ToString().IndexOf('\\'); // Directory.Delete(this.dir + "\\" + row["Value"].ToString().Substring(0, index)); // } // cacheModel.LatestPutTime = DateTime.Now; // Update(type, cacheModel, DateTime.MinValue, ""); // count++; // } // catch (Exception ex) // { // Lin.Core.Controls.TaskbarNotifierUtil.Show(new LinException(-0x2002006, ex)); // } // break; // } // } // if (count <= 0) // { // cacheModel.LatestPutTime = DateTime.Now; // Insert(type, cacheModel); // } //} //return cacheModel.Key; return null; }
/// <summary> /// 写入文件信息 /// </summary>_ /// <param name="file">文件对象</param> /// <param name="key">传过来的键</param> /// <returns>新的键</returns> public string Put(FileInfo file, string key = null, bool isPermanency = false) { if (file == null) { return null; } CacheModel cacheModel = new CacheModel(); lock (AsyncLock) { if (!System.IO.Directory.Exists(dir.ToString()) || !File.Exists(file.FullName)) { return null; } else { try { FileInfo f = new FileInfo(this.dir.FullName + "\\FileInfo-" + DateTime.Now.Ticks + file.Extension); //FileInfo f = new FileInfo(this.dir.FullName + "\\FileInfo-" + DateTime.Now.Ticks); file.CopyTo(Path.Combine(dir.FullName, f.Name), true); // 复制文件 if (key == null) { key = "key-" + DateTime.Now.Ticks; } cacheModel.OldDirectory = file.DirectoryName; cacheModel.Value = f.Name; cacheModel.Key = key; cacheModel.IsPermanency = isPermanency; cacheModel.Size = f.Length.ToString(); key = OperationDB(Type.File, cacheModel); } catch (Exception ex) { Lin.Core.Controls.TaskbarNotifierUtil.Show(new LinException(-0x2002004, ex)); } return key; } } }
/// <summary> /// 写入文件夹 /// </summary> /// <param name="directory">文件目录</param> /// <param name="key">传过来的键</param> /// <param name="isPermanency">上方永久缓存</param> /// <param name="isRecursive">是否复制文件目录下的子目录</param> /// <returns>返回服务器上文件夹相对应的Key值</returns> public string Put(DirectoryInfo directory, string key = null, bool isPermanency = false, bool isRecursive = false) { if (directory == null || string.IsNullOrEmpty(directory.FullName)) { return null; } CacheModel cacheModel = new CacheModel(); lock (AsyncLock) { // 被缓存的目录是否在缓存目录的tmpDir目录下 if (!directory.FullName.StartsWith(TmpDir.FullName + @"\")) { //被缓存的目录在缓存的目录之下 if (directory.FullName.StartsWith(this.dir.FullName + @"\")) { return null; } // 缓存的目录在被缓存的目录下 if (this.dir.FullName.StartsWith(directory.FullName + @"\")) { return null; } } if (!directory.Exists) { return null; } else { try { // 从一个目录将其内容复制到另一目录 string value = "DirectoryInfo-" + DateTime.Now.Ticks + "\\" + directory.Name; CopyFolderTo(Path.Combine(directory.FullName), Path.Combine(dir.FullName + "\\" + value), isRecursive); if (key == null) { key = "key-" + DateTime.Now.Ticks; } cacheModel.Value = value; cacheModel.Key = key; cacheModel.OldDirectory = directory.FullName; cacheModel.IsPermanency = isPermanency; cacheModel.Size = (DirSize(directory) / 1024).ToString(); key = OperationDB(Type.Folder, cacheModel); } catch (Exception e) { Lin.Core.Controls.TaskbarNotifierUtil.Show(new LinException(-0x2002005, e)); } return key; } } }
/// <summary> /// 写入Object对象 /// </summary> /// <param name="obj">对象</param> /// <param name="key">源键</param> /// <returns>产生的新键</returns> public string Put(object obj, string key = null, bool isPermanency = false, bool isCrossDomain = false) { if (obj == null) { return null; } CacheModel cacheModel = new CacheModel(); lock (AsyncLock) { object[] atrrs = obj.GetType().GetCustomAttributes(typeof(global::System.SerializableAttribute), true); // 查找是否存在标记为Serialization的对象 if (atrrs.Length > 0) { try { if (System.IO.Directory.Exists(dir.ToString())) { try { FileInfo f = new FileInfo(this.dir.FullName + "\\ObjectFile-" + DateTime.Now.Ticks + ".tmp"); FileStream fs = f.Create(); if (key == null) { key = "key-" + DateTime.Now.Ticks; } cacheModel.Value = f.Name; cacheModel.Key = key; cacheModel.OldDirectory = ""; cacheModel.LatestPutTime = DateTime.Now; cacheModel.IsPermanency = isPermanency; cacheModel.Size = f.Length.ToString(); key = OperationDB(Type.Obj, cacheModel); // 数据库操作 BinaryFormatter sl = new BinaryFormatter(); sl.Serialize(fs, obj); fs.Close(); System.Threading.Thread.Sleep(1); } catch (Exception ex) { //Lin.Core.Controls.TaskbarNotifierUtil.Show(new AdException(-0x2002002, ex)); } } else { Lin.Core.Controls.TaskbarNotifierUtil.Show("读取的文件对象不存在", LogLevel.INFO, "温馨提示"); } } catch (Exception ex) { Lin.Core.Controls.TaskbarNotifierUtil.Show(new LinException(-0x2002003, ex)); } } return key; } }