示例#1
0
        /// <summary>
        /// Searches the cache of StreamWriters and disposes and removes any that haven't been used accessed since
        /// the notUsedSince parameter.
        /// </summary>
        /// <param name="notUsedSince">The time </param>
        public virtual void ClearOldStreams(DateTime notUsedSince)
        {
            Queue <string> _removals = new Queue <string>();

            using (Locker.AcquireWriterLock(_lock))
            {
                foreach (KeyValuePair <string, CachedStream> kvp in _streamCache)
                {
                    if (kvp.Value.LastAccessTime < notUsedSince)
                    {
                        kvp.Value.StreamWriter.Dispose();
                        // can't remove items whilst enumerating so remember them for later
                        _removals.Enqueue(kvp.Key);
                    }
                }

                while (_removals.Count > 0)
                {
                    _streamCache.Remove(_removals.Dequeue());
                }
            }
        }
示例#2
0
        /// <summary>
        /// Return a stream writer for the passed path name
        /// </summary>
        /// <param name="path">The path name</param>
        /// <returns>An existing or new stream writer</returns>
        public StreamWriter GetStreamWriter(string path)
        {
            CachedStream cs = null;

            using (Locker.AcquireReaderLock(_lock))
            {
                if (!_streamCache.TryGetValue(path, out cs))
                {
                    using (Locker.UpgradeToWriterLock(_lock))
                    {
                        Stream stream = GetStream(path);
                        cs = new CachedStream()
                        {
                            StreamWriter = new StreamWriter(stream)
                        };
                        _streamCache.Add(path, cs);
                    }
                }
            }

            cs.LastAccessTime = DateTime.Now;
            return(cs.StreamWriter);
        }