-
Notifications
You must be signed in to change notification settings - Fork 1.4k
CacheBase / InMemory Storage concurrency #925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@IbraheemOsama as discussed before, this PR contains changes to ensure thread concurrency. I myself encountered item already added exception with CacheBase. |
Add Created property to InMemoryStorageItem Use Created property when removing additional items from InMemoryStorage
|
@Odonno can you please review the update ? |
| { | ||
| _concurrentTasks.Add(fileName, request); | ||
| } | ||
| _concurrentTasks.TryAdd(fileName, request); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try add without proper handling will sallow the operation, I mean what will happen if try add failed ? we should handle that or propagate the problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
of course TryRemove should be handled also
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
Use Index to set the cache request. Use linq to query for expired items
| _concurrentTasks.Remove(fileName); | ||
| } | ||
| } | ||
| _concurrentTasks.TryRemove(fileName, out request); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to try again if the remove fails
| } | ||
| InMemoryStorageItem<T> tempItem = null; | ||
|
|
||
| _inMemoryStorage.TryRemove(key, out tempItem); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if it fails?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool..so it cannot fail due to async reason :)
| } | ||
|
|
||
| _inMemoryStorage.Remove(key); | ||
| _inMemoryStorage.TryRemove(id, out tempItem); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would filename be null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it can this bit is the least of our worries
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was mentioning the potential fail of TryRemove due to async op but after reading the link you sent, I'm ok this is fine!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes these concurrent types need careful looking into 🙂
shenchauhan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A nice update
|
|
||
| private Dictionary<string, ConcurrentRequest> _concurrentTasks = new Dictionary<string, ConcurrentRequest>(); | ||
| private ConcurrentDictionary<string, ConcurrentRequest> _concurrentTasks = new ConcurrentDictionary<string, ConcurrentRequest>(); | ||
| private object _concurrencyLock = new object(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this. It's no longer in use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good spot
| { | ||
| lock (this) | ||
| foreach (var key in keys) | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
key can still be null so need some code to check for it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good spot thought you meant in cache base. Muppet (me)
|
|
||
| // clears expired items in in-memory cache | ||
| var keysToDelete = new List<object>(); | ||
| DateTime expirationDate = DateTime.Now.Subtract(duration); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UTCNow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't want a fight with date time. Local time. Meh
Use ConcurrentDictionary instead of Dictionary in CacheBase
Use ConcurrentDictionary instead of OrderedDictionary in InMemoryStorage