Thread safe
See original GitHub issueCurrently the Dictionary cache is not thread safe. You can replace the current code with the following to correct
`using System; using System.Collections.Concurrent; using System.Collections.Generic;
namespace DeviceDetectorNET.Cache { public class DictionaryCache : ICache { private static readonly Object LockObj = new Object(); private static ConcurrentDictionary<string,object> _staticCache = new ConcurrentDictionary<string, object>(); public bool Contains(string id) { return _staticCache !=null && _staticCache.Keys.Count > 0 && _staticCache.ContainsKey(id); } public bool Delete(string id) { object obj; _staticCache.TryRemove(id, out obj); return true; }
public object Fetch(string id)
{
return Contains(id) ? _staticCache[id] : null;
}
public bool FlushAll()
{
_staticCache = new ConcurrentDictionary<string, object>();
return true;
}
public bool Save(string id, object data, int lifeTime = 0)
{
if (Contains(id))
{
_staticCache[id] = data;
}
else
{
_staticCache.TryAdd(id, data);
}
return true;
}
}
}`
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Thread safety - Wikipedia
Thread safety is a computer programming concept applicable to multi-threaded code. Thread-safe code only manipulates shared data structures in a manner that ...
Read more >What is the meaning of the term "thread-safe"?
As others have pointed out, thread safety means that a piece of code will work without errors if it's used by more than...
Read more >Thread Safety (Multithreaded Programming Guide)
Thread safety is the avoidance of data races--situations in which data are set to either correct or incorrect values, depending upon the order...
Read more >What Is Thread-Safety and How to Achieve It?
This means that different threads can access the same resources without exposing erroneous behavior or producing unpredictable results. This ...
Read more >Thread Safety in Java
Thread safety in java is the process to make our program safe to use in multithreaded environment, there are different ways through which...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @hugoqribeiro , The .NetCore repo is fork from this repo, is not maintained from us. Here is one pull request to port this library to core but is some issues if i do this: here
Hi @hugoqribeiro, this library now is also supported .net core if you want to use it.