├── .gitignore ├── README.md ├── RedisRepository ├── RedisRepository.Test │ └── RedisRepository.Test.csproj.user ├── RedisRepository.sln └── RedisRepository │ ├── Helpers │ └── ConfigurationHelper.cs │ ├── IRedisClient.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── RedisClient.cs │ ├── RedisClientConfigurations.cs │ ├── RedisRepository.csproj │ └── packages.config └── redisrepository └── RedisRepository.Test ├── ArticleDocument.cs ├── Properties └── AssemblyInfo.cs ├── RedisClientTest.cs └── RedisRepository.Test.csproj /.gitignore: -------------------------------------------------------------------------------- 1 | RedisRepository/.vs 2 | RedisRepository/packages/ 3 | RedisRepository/RedisRepository/bin/ 4 | RedisRepository/RedisRepository/obj/ 5 | RedisRepository/RedisRepository.Test/bin/ 6 | RedisRepository/RedisRepository.Test/obj/ 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

RedisRepository

2 | 3 | - RedisRepository is a general purpose redis client for .NET languages using StackExchange.Redis library 4 | 5 |

Features

6 | 7 | - Add generic type to redis. 8 | - Add generic List to redis. 9 | - Remove value. 10 | - Remove multiple values. 11 | - Search 12 | 13 | 14 |

Questions and Contributions

15 | 16 | - If you found a bug or have a feature request, please report an issue or a pull request. 17 | - If you have a question, feel free to contact me (a.raghebmail@gmail.com) 18 | -------------------------------------------------------------------------------- /RedisRepository/RedisRepository.Test/RedisRepository.Test.csproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | ProjectFiles 5 | 6 | -------------------------------------------------------------------------------- /RedisRepository/RedisRepository.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26730.12 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RedisRepository", "RedisRepository\RedisRepository.csproj", "{EED793A6-C472-4043-AA50-37A07E96839E}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RedisRepository.Test", "RedisRepository.Test\RedisRepository.Test.csproj", "{935AC52B-E3F4-4B4F-B9CB-DF4BFE45B855}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {EED793A6-C472-4043-AA50-37A07E96839E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {EED793A6-C472-4043-AA50-37A07E96839E}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {EED793A6-C472-4043-AA50-37A07E96839E}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {EED793A6-C472-4043-AA50-37A07E96839E}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {935AC52B-E3F4-4B4F-B9CB-DF4BFE45B855}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {935AC52B-E3F4-4B4F-B9CB-DF4BFE45B855}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {935AC52B-E3F4-4B4F-B9CB-DF4BFE45B855}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {935AC52B-E3F4-4B4F-B9CB-DF4BFE45B855}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {4A3216EE-FA49-4742-86F9-CA57F9175A92} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /RedisRepository/RedisRepository/Helpers/ConfigurationHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Configuration; 3 | 4 | namespace RedisRepository.Helpers 5 | { 6 | public static class ConfigurationHelper 7 | { 8 | internal static T Get(string appSettingsKey, T defaultValue) 9 | { 10 | string text = ConfigurationManager.AppSettings[appSettingsKey]; 11 | if (string.IsNullOrWhiteSpace(text)) 12 | return defaultValue; 13 | try 14 | { 15 | var value = Convert.ChangeType(text, typeof(T)); 16 | return (T)value; 17 | } 18 | catch 19 | { 20 | return defaultValue; 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /RedisRepository/RedisRepository/IRedisClient.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using StackExchange.Redis; 4 | 5 | namespace RedisRepository 6 | { 7 | public interface IRedisClient 8 | { 9 | bool Remove(string key); 10 | void Remove(RedisKey[] keys); 11 | bool Exists(string key); 12 | void Stop(); 13 | bool Add(string key, object value, TimeSpan expiresAt); 14 | bool Add(string key, T value, TimeSpan expiresAt) where T : class; 15 | bool Update(string key, T value) where T : class; 16 | T Get(string key) where T : class; 17 | List GetList(string key) where T : class; 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /RedisRepository/RedisRepository/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("RedisRepository")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("RedisRepository")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("eed793a6-c472-4043-aa50-37a07e96839e")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /RedisRepository/RedisRepository/RedisClient.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Newtonsoft.Json; 5 | using StackExchange.Redis; 6 | 7 | namespace RedisRepository 8 | { 9 | public class RedisClient : IRedisClient 10 | { 11 | private readonly IDatabase _db; 12 | private readonly ConnectionMultiplexer _redis; 13 | 14 | public RedisClient() 15 | { 16 | const string configuration = "{0},abortConnect=false,defaultDatabase={1},ssl=false,ConnectTimeout={2},allowAdmin=true,connectRetry={3}"; 17 | _redis = ConnectionMultiplexer 18 | .Connect(string.Format(configuration, RedisClientConfigurations.Url, 19 | RedisClientConfigurations.DefaultDatabase, RedisClientConfigurations.ConnectTimeout, 20 | RedisClientConfigurations.ConnectRetry)); 21 | _redis.PreserveAsyncOrder = RedisClientConfigurations.PreserveAsyncOrder; 22 | _db = _redis.GetDatabase(); 23 | } 24 | 25 | /// 26 | /// Remove value from redis 27 | /// 28 | /// 29 | /// 30 | public bool Remove(string key) 31 | { 32 | return _db.KeyDelete(key); 33 | } 34 | 35 | /// 36 | /// Remove multiple values from redis 37 | /// 38 | /// 39 | public void Remove(RedisKey[] keys) 40 | { 41 | _db.KeyDelete(keys); 42 | } 43 | 44 | /// 45 | /// Check if key is exist in redis 46 | /// 47 | /// 48 | /// 49 | public bool Exists(string key) 50 | { 51 | return _db.KeyExists(key); 52 | } 53 | 54 | /// 55 | /// Dispose DB connection 56 | /// 57 | public void Stop() 58 | { 59 | _redis.Dispose(); 60 | } 61 | 62 | /// 63 | /// Add new record in redis 64 | /// 65 | /// generic refrence type 66 | /// unique key of value 67 | /// value of key of type T 68 | /// time span of expiration 69 | /// true or false 70 | public bool Add(string key, T value, TimeSpan expiresAt) where T : class 71 | { 72 | var stringContent = SerializeContent(value); 73 | return _db.StringSet(key, stringContent, expiresAt); 74 | } 75 | 76 | /// 77 | /// Add new record in redis 78 | /// 79 | /// generic refrence type 80 | /// unique key of value 81 | /// value of key of type object 82 | /// time span of expiration 83 | /// true or false 84 | public bool Add(string key, object value, TimeSpan expiresAt) 85 | { 86 | var stringContent = SerializeContent(value); 87 | return _db.StringSet(key, stringContent, expiresAt); 88 | } 89 | 90 | /// 91 | /// Add new record in redis 92 | /// 93 | /// generic refrence type 94 | /// unique key of value 95 | /// value of key of type T 96 | /// true or false 97 | public bool Update(string key, T value) where T : class 98 | { 99 | var stringContent = SerializeContent(value); 100 | return _db.StringSet(key, stringContent); 101 | } 102 | 103 | /// 104 | /// Get value of key, return one object 105 | /// 106 | /// 107 | /// 108 | /// 109 | public T Get(string key) where T : class 110 | { 111 | try 112 | { 113 | RedisValue myString = _db.StringGet(key); 114 | if (myString.HasValue && !myString.IsNullOrEmpty) 115 | { 116 | return DeserializeContent(myString); 117 | } 118 | 119 | return null; 120 | } 121 | catch (Exception) 122 | { 123 | // Log Exception 124 | return null; 125 | } 126 | } 127 | 128 | /// 129 | /// Get all values of key, return list as you can send key in pattern format 130 | /// (article:*) get all articles. 131 | /// 132 | /// 133 | /// 134 | /// 135 | public List GetList(string key) where T : class 136 | { 137 | try 138 | { 139 | var server = _redis.GetServer(host: RedisClientConfigurations.Url, 140 | port: RedisClientConfigurations.Port); 141 | var keys = server.Keys(_db.Database, key); 142 | var keyValues = _db.StringGet(keys.ToArray()); 143 | 144 | var values = (from redisValue in keyValues 145 | where redisValue.HasValue && !redisValue.IsNullOrEmpty 146 | select DeserializeContent(redisValue)).ToList(); 147 | 148 | 149 | return values; 150 | } 151 | catch (Exception) 152 | { 153 | // Log Exception 154 | return null; 155 | } 156 | } 157 | 158 | 159 | #region private 160 | 161 | // serialize and Deserialize content in separate functions as redis can save value as array of binary. 162 | // so, any time you need to change the way of handling value, do it here. 163 | 164 | private string SerializeContent(object value) 165 | { 166 | return JsonConvert.SerializeObject(value); 167 | } 168 | 169 | private T DeserializeContent(RedisValue myString) 170 | { 171 | return JsonConvert.DeserializeObject(myString); 172 | } 173 | 174 | 175 | #endregion 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /RedisRepository/RedisRepository/RedisClientConfigurations.cs: -------------------------------------------------------------------------------- 1 | using RedisRepository.Helpers; 2 | 3 | namespace RedisRepository 4 | { 5 | public static class RedisClientConfigurations 6 | { 7 | private static string _url = ConfigurationHelper.Get("RedisServer", "127.0.0.1"); 8 | public static string Url 9 | { 10 | get { return _url; } 11 | set { _url = value; } 12 | } 13 | 14 | private static int _port = 6379; 15 | public static int Port 16 | { 17 | get { return _port; } 18 | set { _port = value; } 19 | } 20 | 21 | private static int _connectTimeout = 10000; 22 | public static int ConnectTimeout 23 | { 24 | get { return _connectTimeout; } 25 | set { _connectTimeout = value; } 26 | } 27 | 28 | private static int _connectRetry = 3; 29 | public static int ConnectRetry 30 | { 31 | get { return _connectRetry; } 32 | set { _connectRetry = value; } 33 | } 34 | 35 | private static int _defaultDatabase = ConfigurationHelper.Get("RedisDataBase", 0); 36 | public static int DefaultDatabase 37 | { 38 | get { return _defaultDatabase; } 39 | set { _defaultDatabase = value; } 40 | } 41 | 42 | private static bool _preserveAsyncOrder = false; 43 | public static bool PreserveAsyncOrder 44 | { 45 | get { return _preserveAsyncOrder; } 46 | set { _preserveAsyncOrder = value; } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /RedisRepository/RedisRepository/RedisRepository.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {EED793A6-C472-4043-AA50-37A07E96839E} 8 | Library 9 | Properties 10 | RedisRepository 11 | RedisRepository 12 | v4.5.2 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | ..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll 35 | 36 | 37 | ..\packages\StackExchange.Redis.1.1.603\lib\net45\StackExchange.Redis.dll 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 67 | -------------------------------------------------------------------------------- /RedisRepository/RedisRepository/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /redisrepository/RedisRepository.Test/ArticleDocument.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace RedisRepository.Test 4 | { 5 | public class ArticleDocument 6 | { 7 | public int Id { get; set; } 8 | public string Title { get; set; } 9 | public string Body { get; set; } 10 | public DateTime CreatedOn { get; set; } 11 | public bool IsPublished { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /redisrepository/RedisRepository.Test/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("RedisRepository.Test")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("RedisRepository.Test")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("935ac52b-e3f4-4b4f-b9cb-df4bfe45b855")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /redisrepository/RedisRepository.Test/RedisClientTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | 4 | namespace RedisRepository.Test 5 | { 6 | [TestClass] 7 | public class RedisClientTest 8 | { 9 | private RedisClient _redisClient; 10 | [TestInitialize] 11 | public void Initialize() 12 | { 13 | _redisClient = new RedisClient(); 14 | } 15 | 16 | protected virtual TimeSpan GetDefaultTimeSpan() 17 | { 18 | return new TimeSpan(1, 0, 0, 0); 19 | } 20 | 21 | [TestMethod] 22 | public void AddArticlesTest() 23 | { 24 | var article = new ArticleDocument 25 | { 26 | Id = 1, 27 | Title = "Test Article 1", 28 | Body = "Body", 29 | CreatedOn = DateTime.Now, 30 | IsPublished = true 31 | }; 32 | 33 | var added = _redisClient.Add("article:" + article.Id, article, GetDefaultTimeSpan()); 34 | Assert.IsTrue(added); 35 | } 36 | 37 | [TestMethod] 38 | public void UpdateArticlesTest() 39 | { 40 | var article = new ArticleDocument 41 | { 42 | Id = 1, 43 | Title = "Test Article 1", 44 | Body = "Body 1", 45 | CreatedOn = DateTime.Now, 46 | IsPublished = true 47 | }; 48 | 49 | var added = _redisClient.Update("article:" + article.Id, article); 50 | Assert.IsTrue(added); 51 | } 52 | 53 | [TestMethod] 54 | public void GetArticleTest() 55 | { 56 | var article = _redisClient.Get("article:1"); 57 | Assert.AreEqual("Test Article 1", article.Title); 58 | } 59 | 60 | [TestMethod] 61 | public void GetAllArticlesTest(int id) 62 | { 63 | var allArticles = _redisClient.GetList("article:*"); 64 | Assert.AreEqual(1, allArticles.Count); 65 | } 66 | 67 | 68 | [TestMethod] 69 | public bool RemoveArticlesTest() 70 | { 71 | return _redisClient.Remove("article:1"); 72 | } 73 | 74 | 75 | 76 | 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /redisrepository/RedisRepository.Test/RedisRepository.Test.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | {935AC52B-E3F4-4B4F-B9CB-DF4BFE45B855} 7 | Library 8 | Properties 9 | RedisRepository.Test 10 | RedisRepository.Test 11 | v4.5.2 12 | 512 13 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 14 | 10.0 15 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 16 | $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages 17 | False 18 | UnitTest 19 | 20 | 21 | 22 | true 23 | full 24 | false 25 | bin\Debug\ 26 | DEBUG;TRACE 27 | prompt 28 | 4 29 | 30 | 31 | pdbonly 32 | true 33 | bin\Release\ 34 | TRACE 35 | prompt 36 | 4 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | {eed793a6-c472-4043-aa50-37a07e96839e} 61 | RedisRepository 62 | 63 | 64 | 65 | 66 | 67 | 68 | False 69 | 70 | 71 | False 72 | 73 | 74 | False 75 | 76 | 77 | False 78 | 79 | 80 | 81 | 82 | 83 | 84 | 91 | --------------------------------------------------------------------------------