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

推荐订阅源

J
Java Code Geeks
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
MyScale Blog
MyScale Blog
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
N
Netflix TechBlog - Medium
阮一峰的网络日志
阮一峰的网络日志
博客园 - Franky
Recent Announcements
Recent Announcements
A
About on SuperTechFans
Stack Overflow Blog
Stack Overflow Blog
The GitHub Blog
The GitHub Blog
D
Docker
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 世之云枭

网站技术分享 网页内容抓取 推广:湖南省德谦新材料有限公司 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();

}