How Search Caching Works
When the user enters query to search:
- The request is divided into an array of tokens
- A unique hash is created for this token array (tokens in alphabetical order, then MD5). This is a unique search identifier.
- Check cache for hash based results
- If the cache does not exist, save the results in the cache using hash
The problem I'm trying to solve
If a user performs a search that takes 10 seconds and they eagerly refresh the page, we donβt want him to run the query again. This should be blocked.
However, if an expensive query is being executed, we do not want to block other users performing less expensive searches.
To solve this problem, I need some locks.
Implementation
Here's how I implemented it now:
private static readonly object MasterManualSearchLock = new object(); private static readonly Dictionary<string, object> ManualSearchLocks = new Dictionary<string, object>();
Questions
- Is this a reasonable implementation?
- Is this thread safe?
- Does removing locks inside the lock itself include?
source share