Click here to Skip to main content
15,893,814 members
Articles / Database Development / SQL Server

LINQ to SQL Transformation: Examples and Source Code

Rate me:
Please Sign up or sign in to vote.
4.71/5 (9 votes)
23 Jan 2008LGPL33 min read 67.3K   825   50  
LINQ to SQL Transformation: Open Source implementation of IQueryable, examples and source code
using System;
using System.Collections.Generic;
using System.Threading;

namespace LinqToSql {
    
    public class ThreadSafeCache<TKey, TValue> {

        private readonly Dictionary<TKey, TValue> theDictionary = new Dictionary<TKey, TValue>();
        private readonly ReaderWriterLock rwl = new ReaderWriterLock();
        private readonly TimeSpan readerLockTimeout = TimeSpan.FromSeconds(1);
        private readonly TimeSpan writerLockTimeout = TimeSpan.FromSeconds(2);

        public ThreadSafeCache() {
        }

        public TValue this[TKey key] {
            get {
                try {
                    rwl.AcquireReaderLock(readerLockTimeout);
                    return theDictionary[key];
                }
                finally {
                    rwl.ReleaseReaderLock();
                }
            }
        }

        public ICollection<TKey> Keys {
            get {
                try {
                    rwl.AcquireReaderLock(readerLockTimeout);
                    return theDictionary.Keys;
                }
                finally {
                    rwl.ReleaseReaderLock();
                }
            }
        }

        public Dictionary<TKey, TValue>.ValueCollection Values {
            get {
                try {
                    rwl.AcquireReaderLock(readerLockTimeout);
                    return theDictionary.Values;
                }
                finally {
                    rwl.ReleaseReaderLock();
                }
            }
        }

        public bool ContainsKey(TKey key) {

            try {
                rwl.AcquireReaderLock(readerLockTimeout);
                return theDictionary.ContainsKey(key);
            }
            finally {
                rwl.ReleaseReaderLock();
            }
        }

        public bool Remove(TKey key) {
            try {
                rwl.AcquireWriterLock(writerLockTimeout);
                return theDictionary.Remove(key);
            }
            finally {
                rwl.ReleaseWriterLock();
            }
        }

        public bool TryGetValue(TKey key, out TValue value) {
            try {
                rwl.AcquireReaderLock(readerLockTimeout);
                return theDictionary.TryGetValue(key, out value);
            }
            finally {
                rwl.ReleaseReaderLock();
            }
        }

        public bool TryAdd(TKey key, TValue value) {
            try {
                rwl.AcquireWriterLock(writerLockTimeout);

                if (theDictionary.ContainsKey(key)) {
                    return false;
                }

                theDictionary.Add(key, value);
                return true;
            }
            finally {
                rwl.ReleaseWriterLock();
            }
        }

        public void Clear() {
            try {
                rwl.AcquireWriterLock(writerLockTimeout);
                theDictionary.Clear();
            }
            finally {
                rwl.ReleaseWriterLock();
            }
        }

        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {

            foreach (KeyValuePair<TKey, TValue> pair in theDictionary) {
                yield return pair;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Technical Lead Olivine Technology
Kenya Kenya
Technical Lead, Olivine Technology - Nairobi, Kenya.

"The bane of productivity: confusing the rituals of work (sitting at your desk by 8:00am, wearing a clean and well pressed business costume etc.) with actual work that produces results."

Watch me!

Comments and Discussions