PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/MalApi/CachingMyAnimeListApi.cs

https://bitbucket.org/LHCGreg/mal-api
C# | 102 lines | 69 code | 9 blank | 24 comment | 4 complexity | 37e4b8773e1891d7fbac2057540ed1bb MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. namespace MalApi
  7. {
  8. /// <summary>
  9. /// This class is thread-safe if the underlying API is. If the expiration time is null, anime lists are cached for the lifetime of
  10. /// the object. Expired cache entries are only actually removed when a new anime list is inserted into the cache. Cache expiration
  11. /// measurement is susceptible to changes to the system clock.
  12. ///
  13. /// This class only caches user anime lists from GetAnimeListForUser(). Other functions are not cached.
  14. /// </summary>
  15. public class CachingMyAnimeListApi : IMyAnimeListApi
  16. {
  17. private IMyAnimeListApi m_underlyingApi;
  18. private bool m_ownUnderlyingApi;
  19. private AnimeListCache m_cache;
  20. public CachingMyAnimeListApi(IMyAnimeListApi underlyingApi, TimeSpan? expiration, bool ownApi = false)
  21. {
  22. m_underlyingApi = underlyingApi;
  23. m_ownUnderlyingApi = ownApi;
  24. m_cache = new AnimeListCache(expiration);
  25. }
  26. public MalUserLookupResults GetAnimeListForUser(string user)
  27. {
  28. Logging.Log.InfoFormat("Checking cache for user {0}.", user);
  29. MalUserLookupResults animeList;
  30. if (m_cache.GetListForUser(user, out animeList))
  31. {
  32. if (animeList != null)
  33. {
  34. Logging.Log.Info("Got anime list from cache.");
  35. return animeList;
  36. }
  37. else
  38. {
  39. // User does not have an anime list/no such user exists
  40. Logging.Log.Info("Cache indicates that user does not have an anime list.");
  41. throw new MalUserNotFoundException(string.Format("No MAL list exists for {0}.", user));
  42. }
  43. }
  44. else
  45. {
  46. Logging.Log.Info("Cache did not contain the anime list.");
  47. try
  48. {
  49. animeList = m_underlyingApi.GetAnimeListForUser(user);
  50. m_cache.PutListForUser(user, animeList);
  51. return animeList;
  52. }
  53. catch (MalUserNotFoundException)
  54. {
  55. // Cache the fact that the user does not have an anime list
  56. m_cache.PutListForUser(user, null);
  57. throw;
  58. }
  59. }
  60. }
  61. public RecentUsersResults GetRecentOnlineUsers()
  62. {
  63. return m_underlyingApi.GetRecentOnlineUsers();
  64. }
  65. public AnimeDetailsResults GetAnimeDetails(int animeId)
  66. {
  67. return m_underlyingApi.GetAnimeDetails(animeId);
  68. }
  69. public void Dispose()
  70. {
  71. m_cache.Dispose();
  72. if (m_ownUnderlyingApi)
  73. {
  74. m_underlyingApi.Dispose();
  75. }
  76. }
  77. }
  78. }
  79. /*
  80. Copyright 2011 Greg Najda
  81. Licensed under the Apache License, Version 2.0 (the "License");
  82. you may not use this file except in compliance with the License.
  83. You may obtain a copy of the License at
  84. http://www.apache.org/licenses/LICENSE-2.0
  85. Unless required by applicable law or agreed to in writing, software
  86. distributed under the License is distributed on an "AS IS" BASIS,
  87. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  88. See the License for the specific language governing permissions and
  89. limitations under the License.
  90. */