PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/GMusic.Core/CachingLayer/MusicUrlManager.cs

https://github.com/0xdeafcafe/gmusic_wp
C# | 164 lines | 143 code | 19 blank | 2 comment | 18 complexity | 9da0d583afa7981eca6cda81d8659aca MD5 | raw file
  1. #if WINDOWS_PHONE
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data.Common;
  6. using System.Data.Linq;
  7. using System.Data.Linq.Mapping;
  8. using System.Linq;
  9. using Microsoft.Phone.Data.Linq;
  10. namespace MVPTracker.Core.CachingLayer
  11. {
  12. public class MusicUrlManager : DataContext
  13. {
  14. public string ConnectionString { get; private set; }
  15. public Table<UrlItem> StorageCache;
  16. public MusicUrlManager(string connectionString)
  17. : base(connectionString)
  18. {
  19. ConnectionString = connectionString;
  20. if (!DatabaseExists())
  21. CreateDatabase();
  22. // Verify Tables
  23. VerifyTable<UrlItem>();
  24. }
  25. public Table<TEntity> VerifyTable<TEntity>() where TEntity : class
  26. {
  27. var table = GetTable<TEntity>();
  28. try
  29. {
  30. // can call any function against the table to verify it exists
  31. table.Any();
  32. }
  33. catch (DbException exception)
  34. {
  35. if (exception.Message.StartsWith("The specified table does not exist."))
  36. {
  37. var databaseSchemaUpdater = this.CreateDatabaseSchemaUpdater();
  38. databaseSchemaUpdater.AddTable<TEntity>();
  39. databaseSchemaUpdater.Execute();
  40. }
  41. else
  42. {
  43. throw;
  44. }
  45. }
  46. return table;
  47. }
  48. public string GetCacheData(string songId)
  49. {
  50. var entry = StorageCache.First(item => item.SongId == songId && item.Expires < DateTime.Now);
  51. return entry == null ? null : entry.Url;
  52. }
  53. public void SetCacheData(string songId, string url, DateTime expires)
  54. {
  55. #region Read And Delete All Entries
  56. var cachedDataInDb = StorageCache.Where(t => t.SongId == songId).ToList();
  57. var cachedDataData = new List<UrlItem>();
  58. try { cachedDataData = new List<UrlItem>(cachedDataInDb); } catch (Exception) { }
  59. StorageCache.DeleteAllOnSubmit(cachedDataData);
  60. #endregion
  61. #region Add New Data
  62. StorageCache.InsertOnSubmit(new UrlItem
  63. {
  64. Expires = expires,
  65. SongId = songId,
  66. Url = url
  67. });
  68. #endregion
  69. Save();
  70. }
  71. public void Save()
  72. {
  73. SubmitChanges();
  74. }
  75. }
  76. [Table]
  77. public class UrlItem : INotifyPropertyChanged, INotifyPropertyChanging
  78. {
  79. private int _id;
  80. [Column(Name = "Id", IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
  81. public int Id
  82. {
  83. get { return _id; }
  84. set
  85. {
  86. if (_id == value) return;
  87. NotifyPropertyChanging("Id");
  88. _id = value;
  89. NotifyPropertyChanged("Id");
  90. }
  91. }
  92. private string _songId;
  93. [Column(Name = "SongId", AutoSync = AutoSync.OnInsert)]
  94. public string SongId
  95. {
  96. get { return _songId; }
  97. set
  98. {
  99. if (_songId == value) return;
  100. NotifyPropertyChanging("SongId");
  101. _songId = value;
  102. NotifyPropertyChanged("SongId");
  103. }
  104. }
  105. private string _url;
  106. [Column(Name = "Url", AutoSync = AutoSync.OnInsert)]
  107. public string Url
  108. {
  109. get { return _url; }
  110. set
  111. {
  112. if (_url == value) return;
  113. NotifyPropertyChanging("Url");
  114. _url = value;
  115. NotifyPropertyChanged("Url");
  116. }
  117. }
  118. private DateTime _expires;
  119. [Column(Name = "Expires", AutoSync = AutoSync.OnInsert)]
  120. public DateTime Expires
  121. {
  122. get { return _expires; }
  123. set
  124. {
  125. if (_expires == value) return;
  126. NotifyPropertyChanging("Expires");
  127. _expires = value;
  128. NotifyPropertyChanged("Expires");
  129. }
  130. }
  131. public event PropertyChangedEventHandler PropertyChanged;
  132. public void NotifyPropertyChanged(String info)
  133. {
  134. if (PropertyChanged != null)
  135. PropertyChanged(this, new PropertyChangedEventArgs(info));
  136. }
  137. public event PropertyChangingEventHandler PropertyChanging;
  138. public void NotifyPropertyChanging(String info)
  139. {
  140. if (PropertyChanging != null)
  141. PropertyChanging(this, new PropertyChangingEventArgs(info));
  142. }
  143. }
  144. }
  145. #endif