惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
WordPress大学
WordPress大学
爱范儿
爱范儿
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
罗磊的独立博客
博客园_首页
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
MyScale Blog
MyScale Blog
IT之家
IT之家
H
Help Net Security
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
人人都是产品经理
人人都是产品经理

博客园 - 世之云枭

网站技术分享 网页内容抓取 推广:湖南省德谦新材料有限公司 Http://www.paint123.cn CentOS MySQL安装和编译【转帖】 缓存容器类的实现(C#) 设计一个高效的网络服务器用户管理类 修改自增字段 C++、Java与C#的命名规范总结 (转) VTL-vm模板的变量用法 - 世之云枭 - 博客园 http://www.cnblogs.com/onlytiancai/archive/2009/04/11/1433456.html 数据库中与c#中的数据类型对照 - 世之云枭 - 博客园 SessionID的正确说明 临时文件 细节决定成败 NVelocity 主从表 自动生成清空数据库的SQL语句 自动生成Insert数据的SQL脚本 分页存储过程 常用的获取最大值
ReadFreeCache
世之云枭 · 2010-02-03 · via 博客园 - 世之云枭

public abstract class ReadFreeCache<TKey, TValue>

{

    

protected ReadFreeCache()

    

protected ReadFreeCache(IEqualityComparer<TKey> comparer)

        

this.m_storage = new Dictionary<TKey, TValue>(comparer);

    

public abstract TValue Create(TKey key);

    

private Dictionary<TKey, TValue> m_storage;

    

private readonly object m_writeLock = new object();

    

public TValue Get(TKey key)

        

if (this.m_storage.TryGetValue(key, out value))

            

if (this.m_storage.TryGetValue(key, out value))

            

value = this.Create(key);

            

var newStorage = this.m_storage.ToDictionary(

                

this.m_storage.Comparer);

            

newStorage.Add(key, value);

            

this.m_storage = newStorage;

}

___________________________________________________________________________

public abstract class ReadWriteCache<TKey, TValue>

{

    

protected ReadWriteCache()

    

protected ReadWriteCache(IEqualityComparer<TKey> comparer)

        

this.m_storage = new Dictionary<TKey, TValue>(comparer);

    

private readonly Dictionary<TKey, TValue> m_storage;

    

private readonly ReaderWriterLockSlim m_rwLock = new ReaderWriterLockSlim();

    

protected abstract TValue Create(TKey key);

    

public TValue Get(TKey key)

        

this.m_rwLock.EnterReadLock();

            

if (this.m_storage.TryGetValue(key, out value))

            

this.m_rwLock.ExitReadLock();

        

this.m_rwLock.EnterWriteLock();

            

if (this.m_storage.TryGetValue(key, out value))

            

value = this.Create(key);

            

this.m_storage.Add(key, value);

            

this.m_rwLock.ExitWriteLock();

}