Example #1
0
        static Cache()
        {
            STEM.Sys.Global.ThreadPool.BeginAsync(new ThreadStart(_TimeoutThread), TimeSpan.FromMinutes(5));

            lock (Cache._CacheObjects)
            {
                if (Cache._CacheObjects.Count == 0)
                {
                    if (Directory.Exists(CacheDirectory))
                    {
                        foreach (string file in Directory.GetFiles(CacheDirectory, "*.cache"))
                        {
                            try
                            {
                                SessionObject so = SessionObject.Deserialize(File.ReadAllText(file));

                                if (so != null)
                                {
                                    _CacheObjects[so.Key.ToUpper(System.Globalization.CultureInfo.CurrentCulture)] = so;
                                }
                            }
                            catch { }
                        }
                    }
                }
            }
        }
        public static SessionObject Deserialize(string xml)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);

                if (doc.DocumentElement.SelectNodes("//VersionDescriptor/AssemblyName").Count == 0 ||
                    doc.DocumentElement.SelectNodes("//VersionDescriptor/TypeName").Count == 0)
                {
                    throw new Exception("The VersionDescriptor tag is missing or malformed.");
                }

                Assembly asm = AppDomain.CurrentDomain.Load(new AssemblyName(doc.DocumentElement.SelectNodes("//VersionDescriptor/AssemblyName")[0].InnerText));

                if (asm == null)
                {
                    throw new Exception("The Assembly:" + doc.DocumentElement.SelectNodes("//VersionDescriptor/AssemblyName")[0].InnerText + " could not be located.");
                }

                Type tgt = asm.GetType(doc.DocumentElement.SelectNodes("//VersionDescriptor/TypeName")[0].InnerText);

                if (tgt == null)
                {
                    throw new Exception("The Type:" + doc.DocumentElement.SelectNodes("//VersionDescriptor/TypeName")[0].InnerText + " could not be located.");
                }

                string valueXml = doc.DocumentElement.SelectSingleNode("Value").InnerXml;

                doc.DocumentElement.RemoveChild(doc.DocumentElement.SelectSingleNode("Value"));

                SessionObject so = STEM.Sys.Serializable.Deserialize(doc.DocumentElement.OuterXml, typeof(SessionObject)) as SessionObject;

                so._Value = STEM.Sys.Serializable.Deserialize(valueXml, tgt);

                return(so);
            }
            catch (Exception ex)
            {
                STEM.Sys.EventLog.WriteEntry("SessionObject.Deserialize", ex.ToString(), STEM.Sys.EventLog.EventLogEntryType.Error);
                return(null);
            }
        }
Example #3
0
        public object this[string key]
        {
            get
            {
                if (String.IsNullOrEmpty(key))
                {
                    throw new ArgumentNullException(nameof(key));
                }

                try
                {
                    if (!_CacheObjects.ContainsKey(key.ToUpper(System.Globalization.CultureInfo.CurrentCulture)))
                    {
                        string file = Path.Combine(CacheDirectory, STEM.Sys.State.KeyManager.GetHash(key) + ".cache");
                        if (File.Exists(file))
                        {
                            try
                            {
                                SessionObject so = SessionObject.Deserialize(File.ReadAllText(file));

                                if (so != null)
                                {
                                    _CacheObjects[so.Key.ToUpper(System.Globalization.CultureInfo.CurrentCulture)] = so;
                                }
                            }
                            catch { }
                        }
                    }

                    if (_CacheObjects.ContainsKey(key.ToUpper(System.Globalization.CultureInfo.CurrentCulture)))
                    {
                        object ret = _CacheObjects[key.ToUpper(System.Globalization.CultureInfo.CurrentCulture)].Value;
                        return(ret);
                    }
                }
                catch { }

                return(null);
            }

            set
            {
                if (String.IsNullOrEmpty(key))
                {
                    throw new ArgumentNullException(nameof(key));
                }

                if (value == null)
                {
                    lock (_CacheObjects)
                        _CacheObjects.Remove(key.ToUpper(System.Globalization.CultureInfo.CurrentCulture));

                    try
                    {
                        if (File.Exists(Path.Combine(CacheDirectory, STEM.Sys.State.KeyManager.GetHash(key) + ".cache")))
                        {
                            File.Delete(Path.Combine(CacheDirectory, STEM.Sys.State.KeyManager.GetHash(key) + ".cache"));
                        }
                    }
                    catch { }
                }
                else
                {
                    lock (_CacheObjects)
                        _CacheObjects[key.ToUpper(System.Globalization.CultureInfo.CurrentCulture)] = new SessionObject(key, value);

                    try
                    {
                        File.WriteAllText(Path.Combine(CacheDirectory, STEM.Sys.State.KeyManager.GetHash(key) + ".cache"), _CacheObjects[key.ToUpper(System.Globalization.CultureInfo.CurrentCulture)].Serialize());
                    }
                    catch (Exception ex)
                    {
                        STEM.Sys.EventLog.WriteEntry("STEM.Sys.State.Cache", new Exception("Failed to write cache file for key (" + key + ")", ex));
                    }
                }
            }
        }