Skip to content

Instantly share code, notes, and snippets.

@cskeppstedt
Created August 13, 2014 16:52
Show Gist options
  • Save cskeppstedt/d8b4b1ab550dc2c49b3f to your computer and use it in GitHub Desktop.
Save cskeppstedt/d8b4b1ab550dc2c49b3f to your computer and use it in GitHub Desktop.
bool TryGetTasksFromCache(int userId = -1, string stage, out List<UserTask> cachedTasks)
{
cachedTasks = DataCache[GetCacheKey(userId, stage)] as List<UserTask>;
return (cachedTasks != null);
}
List<UserTask> GetTasks(int userId = -1, string stage, List<Severity> severities = null)
{
List<UserTask> listOfUserTasks;
if (TryGetTasksFromCache(userId, stage, out listOfUserTasks))
return listOfUserTasks;
listOfUserTasks = GetTasksNotFromCache(userId, stage, severities);
SetDataCache(userId, stage, listOfUserTasks);
return listOfUserTasks;
}
void ClearDataCache(int userId, string stage)
{
DataCache.Remove(GetCacheKey(userId, stage));
}
void SetDataCache(int userId, string stage, List<UserTask> tasks)
{
// cache policy
var policy = new CacheItemPolicy
{
AbsoluteExpiration = DateTimeOffset.UtcNow.AddSeconds(DATA_CACHE_NUMBER_OF_SECONDS)
};
// storing data in cache.
DataCache.Set(GetCacheKey(userId, stage), tasks, policy);
}
string GetCacheKey(int userId, string stage)
{
return "listOfBaseTasks" + userId + stage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment