コード例 #1
0
        public bool SaveObject(string persistenceId, string persistenceContext, string objectContent)
        {
            bool retVal = false;

            try
            {
                if (string.IsNullOrEmpty(persistenceContext))
                {
                    persistenceContext = "*";
                }

                string key          = BuildCacheKey(persistenceId, persistenceContext);
                bool   foundInCache = false;

                lock (_cachePollerLock)
                {
                    // Is the requested object in the cache ?
                    if (_cache.ContainsKey(key))
                    {
                        Logger.LogTrace("SaveObject: Object with key {0} was found and updated in cache.", key);

                        // If it is, update it in the cache.
                        _cache[key].Value = objectContent;
                        foundInCache      = true;

                        retVal = true;
                    }
                }

                // We need to update in DB anyways and also make sure it is in the cache.
                if (foundInCache == false)
                {
                    CacheItem ci = new CacheItem(key, objectContent);

                    lock (_cachePollerLock)
                    {
                        Logger.LogTrace("SaveObject: Object with key {0} was not found cache => adding it now", key);
                        _cache.Add(key, ci);

                        retVal = true;
                    }
                }

                ThreadPool.QueueUserWorkItem((c) =>
                {
                    Logger.LogTrace("SaveObject: Object with key {0} is now saved also in DB.", key);
                    DbStore.SaveObject(persistenceId, persistenceContext, objectContent);
                });
            }
            catch
            {
                retVal = false;
            }

            return(retVal);
        }
コード例 #2
0
        public string ReadObject(string persistenceId, string persistenceContext)
        {
            if (string.IsNullOrEmpty(persistenceContext))
            {
                persistenceContext = "*";
            }

            string key = BuildCacheKey(persistenceId, persistenceContext);

            lock (_cachePollerLock)
            {
                // Is the requested object in the cache ?
                if (_cache.ContainsKey(key))
                {
                    Logger.LogTrace("ReadObject: Object with key {0} was found in cache.", key);

                    // If it is, return it from the cache.
                    return(_cache[key].Value);
                }
            }

            Logger.LogTrace("ReadObject: Object with key {0} was not found in cache.", key);

            // If it is not, get it from the persistence DB and also add it in the cache.
            string s = DbStore.ReadObject(persistenceId, persistenceContext);

            if (s != null)
            {
                Logger.LogTrace("ReadObject: Object with key {0} was found in DB.", key);

                CacheItem ci = new CacheItem(key, s);

                lock (_cachePollerLock)
                {
                    Logger.LogTrace("ReadObject: Object with key {0} was added to cache with a TTL of 5 sec", key);
                    _cache.Add(key, ci);
                }

                return(ci.Value);
            }

            Logger.LogTrace("ReadObject: Object with key {0} was not found in DB also.", key);

            return(null);
        }
コード例 #3
0
        public bool DeleteObject(string persistenceId, string persistenceContext)
        {
            bool retVal = false;

            try
            {
                if (string.IsNullOrEmpty(persistenceContext))
                {
                    persistenceContext = "*";
                }

                string key = BuildCacheKey(persistenceId, persistenceContext);

                lock (_cachePollerLock)
                {
                    if (_cache.ContainsKey(key))
                    {
                        Logger.LogTrace("DeleteObject: Object with key {0} was found in cache and removed.", key);
                        _cache.Remove(key);

                        retVal = true;
                    }
                }

                ThreadPool.QueueUserWorkItem((c) =>
                {
                    Logger.LogTrace("DeleteObject: Object with key {0} is now removed also from the DB.", key);
                    DbStore.DeleteObject(persistenceId, persistenceContext);
                });
            }
            catch
            {
                retVal = false;
            }

            return(retVal);
        }