예제 #1
0
        public bool IsCacheExists(string applicationName, string[] keyPath)
        {
            string[] keys = new string[] { applicationName }.Concat(keyPath).ToArray();
            string[] hexs = keys.Select(k => KeyPathGenerator.GenerateKey(k)).ToArray();
            lock (indexXml)
            {
                XElement currentElement = indexXml;
                for (int i = 0; i < keys.Length; i++)
                {
                    string   hex        = hexs[i];
                    XElement keyElement = currentElement.Element(hex);
                    if (keyElement == null)
                    {
                        return(false);
                    }
                    currentElement = keyElement;
                }

                XElement contentElement = currentElement.Element("Content");
                if (contentElement == null)
                {
                    return(false);
                }

                return(true);
            }
        }
예제 #2
0
        public string GetCache(string applicationName, string[] keyPath, bool exceptionIfNotExists)
        {
            string key = KeyPathGenerator.GenerateKey(keyPath);

            lock (storages)
            {
                Dictionary <string, StorageContent> storage = null;
                if (!storages.TryGetValue(applicationName, out storage))
                {
                    if (exceptionIfNotExists)
                    {
                        throw new InvalidOperationException("Application not exists.");
                    }
                    else
                    {
                        return("");
                    }
                }

                StorageContent storageContent = null;
                if (!storage.TryGetValue(key, out storageContent))
                {
                    if (exceptionIfNotExists)
                    {
                        throw new InvalidOperationException("Key path not exists.");
                    }
                    else
                    {
                        return("");
                    }
                }

                return(storageContent.Content);
            }
        }
예제 #3
0
        public void SetCache(string applicationName, string[] keyPath, string content, bool exceptionIfNotExists)
        {
            string key = KeyPathGenerator.GenerateKey(keyPath);

            lock (storages)
            {
                Dictionary <string, StorageContent> storage = null;
                if (!storages.TryGetValue(applicationName, out storage))
                {
                    if (exceptionIfNotExists)
                    {
                        throw new InvalidOperationException("Application not exists.");
                    }
                    storage = new Dictionary <string, StorageContent>();
                    storages.Add(applicationName, storage);
                }

                StorageContent storageContent = null;
                if (!storage.TryGetValue(key, out storageContent))
                {
                    if (exceptionIfNotExists)
                    {
                        throw new InvalidOperationException("Key path not exists.");
                    }
                    storageContent         = new StorageContent();
                    storageContent.KeyPath = keyPath;
                    storage.Add(key, storageContent);
                }

                storageContent.Content = content;
            }
        }
예제 #4
0
        public bool IsCacheExists(string applicationName, string[] keyPath)
        {
            string key = KeyPathGenerator.GenerateKey(keyPath);

            lock (storages)
            {
                Dictionary <string, StorageContent> storage = null;
                if (!storages.TryGetValue(applicationName, out storage))
                {
                    return(false);
                }

                return(storage.ContainsKey(key));
            }
        }
예제 #5
0
 public string[][] GetKeyPaths(string applicationName)
 {
     lock (indexXml)
     {
         string   hex     = KeyPathGenerator.GenerateKey(applicationName);
         XElement element = indexXml.Element(hex);
         if (element != null)
         {
             List <string[]> allKeyPaths = new List <string[]>();
             CollectKeyPaths(element, new List <string>(), allKeyPaths);
             return(allKeyPaths.ToArray());
         }
         else
         {
             return(new string[][] { });
         }
     }
 }
예제 #6
0
        public void SetCache(string applicationName, string[] keyPath, string content, bool exceptionIfNotExists)
        {
            string[] keys = new string[] { applicationName }.Concat(keyPath).ToArray();
            string[] hexs = keys.Select(k => KeyPathGenerator.GenerateKey(k)).ToArray();
            lock (indexXml)
            {
                XElement currentElement = indexXml;
                for (int i = 0; i < keys.Length; i++)
                {
                    string   hex        = hexs[i];
                    XElement keyElement = currentElement.Element(hex);
                    if (keyElement == null)
                    {
                        if (exceptionIfNotExists)
                        {
                            throw new InvalidOperationException(i == 0 ? "Application not exists." : "Key path not exists.");
                        }
                        keyElement = new XElement(hex, new XElement("Key", new XCData(keys[i])));
                        currentElement.Add(keyElement);
                    }
                    currentElement = keyElement;
                }

                XElement contentElement = currentElement.Element("Content");
                if (contentElement == null)
                {
                    if (exceptionIfNotExists)
                    {
                        throw new InvalidOperationException(keyPath.Length == 0 ? "Application not exists." : "Key path not exists.");
                    }
                    contentElement = new XElement("Content", Guid.NewGuid().ToString());
                    currentElement.Add(contentElement);
                }

                string guid = contentElement.Value;
                File.WriteAllText(GetDataFile(guid), content);
                indexXml.Save(IndexPath);
            }
        }
예제 #7
0
        public string GetCacheFullPath(string applicationName, string[] keyPath, bool exceptionIfNotExists)
        {
            string[] keys = new string[] { applicationName }.Concat(keyPath).ToArray();
            string[] hexs = keys.Select(k => KeyPathGenerator.GenerateKey(k)).ToArray();
            lock (indexXml)
            {
                XElement currentElement = indexXml;
                for (int i = 0; i < keys.Length; i++)
                {
                    string   hex        = hexs[i];
                    XElement keyElement = currentElement.Element(hex);
                    if (keyElement == null)
                    {
                        if (exceptionIfNotExists)
                        {
                            throw new InvalidOperationException(i == 0 ? "Application not exists." : "Key path not exists.");
                        }
                        return("");
                    }
                    currentElement = keyElement;
                }

                XElement contentElement = currentElement.Element("Content");
                if (contentElement == null)
                {
                    if (exceptionIfNotExists)
                    {
                        throw new InvalidOperationException(keyPath.Length == 0 ? "Application not exists." : "Key path not exists.");
                    }
                    return("");
                }

                string guid = contentElement.Value;
                return(GetDataFile(guid));
            }
        }