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

推荐订阅源

K
Kaspersky official blog
云风的 BLOG
云风的 BLOG
IT之家
IT之家
T
The Blog of Author Tim Ferriss
C
Check Point Blog
N
Netflix TechBlog - Medium
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
G
Google Developers Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
F
Fortinet All Blogs
D
DataBreaches.Net
The Register - Security
The Register - Security
L
LINUX DO - 最新话题
W
WeLiveSecurity
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V2EX - 技术
V2EX - 技术
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
F
Full Disclosure
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
I
InfoQ
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
MyScale Blog
MyScale Blog
AI
AI
Recent Announcements
Recent Announcements
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
Vulnerabilities – Threatpost
NISL@THU
NISL@THU
SecWiki News
SecWiki News
Cisco Talos Blog
Cisco Talos Blog
H
Heimdal Security Blog
Y
Y Combinator Blog
N
News | PayPal Newsroom
P
Privacy International News Feed
美团技术团队
Attack and Defense Labs
Attack and Defense Labs
D
Docker
PCI Perspectives
PCI Perspectives
Webroot Blog
Webroot Blog
A
About on SuperTechFans
A
Arctic Wolf
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
MongoDB | Blog
MongoDB | Blog
T
Threat Research - Cisco Blogs

博客园 - 世之云枭

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

}