Beispiel #1
0
        // Fetch an object from backing store if not cached, serve from
        // cache if it is.
        //
        public virtual Object Get(string index, FetchDelegate fetch)
        {
            CacheItemBase item = GetItem(index);

            if (item != null)
            {
                return(item.Retrieve());
            }

            Object data = fetch(index);

            if (data == null)
            {
                if ((m_Flags & CacheFlags.CacheMissing) != 0)
                {
                    lock (m_Index)
                    {
                        CacheItemBase missing = new CacheItemBase(index);
                        if (!m_Index.Contains(missing))
                        {
                            m_Index.Add(missing);
                            m_Lookup[index] = missing;
                        }
                    }
                }
                return(null);
            }

            Store(index, data);
            return(data);
        }
Beispiel #2
0
        // Get an item from cache. Return the raw item, not it's data
        //
        protected virtual CacheItemBase GetItem(string index)
        {
            CacheItemBase item = null;

            lock (m_Index)
            {
                if (m_Lookup.ContainsKey(index))
                {
                    item = m_Lookup[index];
                }

                if (item == null)
                {
                    Expire(true);
                    return(null);
                }

                item.hits++;
                item.lastUsed = DateTime.UtcNow;

                Expire(true);
            }

            return(item);
        }
Beispiel #3
0
        // Get an item from cache. Do not try to fetch from source if not
        // present. Just return null
        //
        public virtual Object Get(string index)
        {
            CacheItemBase item = GetItem(index);

            if (item == null)
            {
                return(null);
            }

            return(item.Retrieve());
        }
Beispiel #4
0
        // Find an object in cache by delegate.
        //
        public Object Find(Predicate <CacheItemBase> d)
        {
            CacheItemBase item = m_Index.Find(d);

            if (item == null)
            {
                return(null);
            }

            return(item.Retrieve());
        }
Beispiel #5
0
        public void Invalidate(string uuid)
        {
            if (!m_Lookup.ContainsKey(uuid))
            {
                return;
            }

            CacheItemBase item = m_Lookup[uuid];

            m_Lookup.Remove(uuid);
            m_Index.Remove(item);
        }
Beispiel #6
0
        // Get an item from cache. Return the raw item, not it's data
        //
        protected virtual CacheItemBase GetItem(string index)
        {
            CacheItemBase item = null;

            m_IndexRwLock.AcquireReaderLock(-1);
            try
            {
                if (m_Lookup.ContainsKey(index))
                {
                    item = m_Lookup[index];
                }

                if (item == null)
                {
                    LockCookie lc = m_IndexRwLock.UpgradeToWriterLock(-1);
                    try
                    {
                        Expire(true);
                    }
                    finally
                    {
                        m_IndexRwLock.DowngradeFromWriterLock(ref lc);
                    }
                    return(null);
                }

                item.hits++;
                item.lastUsed = DateTime.Now;

                {
                    LockCookie lc = m_IndexRwLock.UpgradeToWriterLock(-1);
                    try
                    {
                        Expire(true);
                    }
                    finally
                    {
                        m_IndexRwLock.DowngradeFromWriterLock(ref lc);
                    }
                }
            }
            finally
            {
                m_IndexRwLock.ReleaseReaderLock();
            }

            return(item);
        }
Beispiel #7
0
        public void Invalidate(string uuid)
        {
            m_IndexRwLock.AcquireWriterLock(-1);
            try
            {
                if (!m_Lookup.ContainsKey(uuid))
                {
                    return;
                }

                CacheItemBase item = m_Lookup[uuid];
                m_Lookup.Remove(uuid);
                m_Index.Remove(item);
            }
            finally
            {
                m_IndexRwLock.ReleaseWriterLock();
            }
        }
Beispiel #8
0
        // Fetch an object from backing store if not cached, serve from
        // cache if it is.
        //
        public virtual Object Get(string index, FetchDelegate fetch)
        {
            Object item = Get(index);

            if (item != null)
            {
                return(item);
            }

            Object data = fetch(index);

            if (data == null)
            {
                if ((m_Flags & CacheFlags.CacheMissing) != 0)
                {
                    m_IndexRwLock.AcquireWriterLock(-1);
                    try
                    {
                        CacheItemBase missing = new CacheItemBase(index);
                        if (!m_Index.Contains(missing))
                        {
                            m_Index.Add(missing);
                            m_Lookup[index] = missing;
                        }
                    }
                    finally
                    {
                        m_IndexRwLock.ReleaseWriterLock();
                    }
                }
                return(null);
            }

            Store(index, data);

            return(data);
        }