Beispiel #1
0
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="objID"></param>
        /// <returns></returns>
        public static object GetObject(string objID)
        {
            if (string.IsNullOrEmpty(objID))
            {
                return(null);
            }
            if (_state == "close")
            {
                return(null);
            }

            string k    = objID.Md5();
            string path = GetCacheFile(k) + k;

            if (File.Exists(path))
            {
                FileInfo i = new FileInfo(path);
                if (i != null)
                {
                    object data = null;
                    io_lock.AcquireReaderLock();
                    try
                    {
                        FileJson j = MFSerializationUtil.Deserialize <FileJson>(File.ReadAllText(i.FullName, Encoding.UTF8));
                        if (j != null)
                        {
                            if (j.ExpireTime.CompareTo(DateTime.Now) < 0)
                            {
                                File.Delete(i.FullName);
                            }

                            else
                            {
                                data = j.Obj;
                            }
                        }
                    }

                    catch
                    { }

                    finally
                    {
                        io_lock.ReleaseReaderLock();
                    }

                    return(data);
                }
            }

            return(null);
        }
Beispiel #2
0
        public static void AddObject(string objID, object obj, int expire)
        {
            if (_state == "close")
            {
                return;
            }
            if (obj != null)
            {
                DateTime expireTime = DateTime.Now.AddSeconds(expire);
                if (expireTime.CompareTo(DateTime.Now) > 0)
                {
                    objID = objID.Md5();
                    FileJson j = new FileJson();
                    j.ExpireTime = expireTime;
                    j.Obj        = obj;

                    string content       = MFSerializationUtil.Serialize <FileJson>(j);
                    string directoryPath = GetCacheFile(objID);
                    if (!Directory.Exists(directoryPath))
                    {
                        Directory.CreateDirectory(directoryPath);
                    }
                    if (!File.Exists(directoryPath + objID))
                    {
                        io_lock.AcquireWriterLock();
                        try
                        {
                            File.WriteAllText(directoryPath + objID, content, Encoding.UTF8);
                        }

                        finally
                        {
                            io_lock.ReleaseWriterLock();
                        }
                    }
                }
            }
        }