Advertisement
Guest User

Untitled

a guest
Jan 24th, 2025
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.IO;
  10. using System.Text.RegularExpressions;
  11. using ZennoLab.CommandCenter;
  12. using ZennoLab.InterfacesLibrary;
  13. using ZennoLab.InterfacesLibrary.ProjectModel;
  14. using ZennoLab.InterfacesLibrary.ProjectModel.Collections;
  15. using ZennoLab.InterfacesLibrary.ProjectModel.Enums;
  16. using ZennoLab.Macros;
  17. using Global.ZennoExtensions;
  18. using ZennoLab.Emulation;
  19. using StackExchange.Redis;
  20.  
  21. namespace ZennoLab.OwnCode
  22. {
  23. /// <summary>
  24. /// A simple class of the common code
  25. /// </summary>
  26. public class CommonCode
  27. {
  28. /// <summary>
  29. /// Lock this object to mark part of code for single thread execution
  30. /// </summary>
  31. public static object SyncObject = new object();
  32.  
  33. // Insert your code here
  34. }
  35.  
  36. public class RedisDB
  37. {
  38. private string connectionString;
  39. private ConnectionMultiplexer redisConnection;
  40.  
  41. public RedisDB(string connectionString)
  42. {
  43. this.connectionString = connectionString;
  44. redisConnection = ConnectionMultiplexer.Connect(connectionString);
  45. }
  46.  
  47. public void Open()
  48. {
  49. if (redisConnection == null || !redisConnection.IsConnected)
  50. {
  51. redisConnection = ConnectionMultiplexer.Connect(connectionString);
  52. }
  53. }
  54.  
  55. public void Close()
  56. {
  57. if (redisConnection != null && redisConnection.IsConnected)
  58. {
  59. redisConnection.Dispose();
  60. redisConnection = null;
  61. }
  62. }
  63.  
  64. /// <summary>
  65. /// Sync a ZennoPoster list to Redis as a list, replacing old data
  66. /// </summary>
  67. /// <param name="redisKey">The Redis list key</param>
  68. /// <param name="project">ZennoPoster project instance</param>
  69. public void SyncZennoListToRedis(string redisKey, IZennoPosterProjectModel project)
  70. {
  71. var db = redisConnection.GetDatabase();
  72.  
  73. // Clear the current Redis list
  74. db.KeyDelete(redisKey);
  75.  
  76. // Add items from ZennoPoster list to Redis
  77. var zennoList = project.Lists["List1"];
  78. foreach (var item in zennoList)
  79. {
  80. db.ListRightPush(redisKey, item);
  81. }
  82. }
  83.  
  84. /// <summary>
  85. /// Add ZennoPoster list items to Redis without removing old data
  86. /// </summary>
  87. /// <param name="redisKey">The Redis list key</param>
  88. /// <param name="project">ZennoPoster project instance</param>
  89. public void AppendZennoListToRedis(string redisKey, IZennoPosterProjectModel project)
  90. {
  91. var db = redisConnection.GetDatabase();
  92.  
  93. // Add items from ZennoPoster list to Redis without deleting old data
  94. var zennoList = project.Lists["List1"];
  95. foreach (var item in zennoList)
  96. {
  97. db.ListRightPush(redisKey, item);
  98. }
  99. }
  100.  
  101. /// <summary>
  102. /// Delete a Redis list by key
  103. /// </summary>
  104. /// <param name="redisKey">The Redis list key to delete</param>
  105. public void DeleteRedisList(string redisKey)
  106. {
  107. var db = redisConnection.GetDatabase();
  108. bool isDeleted = db.KeyDelete(redisKey);
  109. if (isDeleted)
  110. {
  111. Console.WriteLine($"Redis list with key '{redisKey}' has been successfully deleted.");
  112. }
  113. else
  114. {
  115. Console.WriteLine($"Redis list with key '{redisKey}' does not exist or could not be deleted.");
  116. }
  117. }
  118.  
  119. /// <summary>
  120. /// Pop a string from Redis list (remove and return it)
  121. /// </summary>
  122. /// <param name="redisKey">The Redis list key</param>
  123. /// <returns>String popped from the Redis list</returns>
  124. public string PopFromRedisList(string redisKey)
  125. {
  126. var db = redisConnection.GetDatabase();
  127. return db.ListLeftPop(redisKey);
  128. }
  129.  
  130. /// <summary>
  131. /// Insert a list of values into Redis
  132. /// </summary>
  133. /// <param name="key">Redis key</param>
  134. /// <param name="values">List of values to insert</param>
  135. public void InsertList(string key, List<string> values)
  136. {
  137. var db = redisConnection.GetDatabase();
  138. foreach (var value in values)
  139. {
  140. db.ListRightPush(key, value);
  141. }
  142. }
  143.  
  144. /// <summary>
  145. /// Get all values from a Redis list
  146. /// </summary>
  147. /// <param name="key">Redis key</param>
  148. /// <returns>List of strings from Redis</returns>
  149. public List<string> GetList(string key)
  150. {
  151. var db = redisConnection.GetDatabase();
  152. var length = db.ListLength(key);
  153. var list = db.ListRange(key, 0, length - 1);
  154. var result = new List<string>();
  155. foreach (var item in list)
  156. {
  157. result.Add(item.ToString());
  158. }
  159. return result;
  160. }
  161.  
  162. /// <summary>
  163. /// Insert a single key-value pair into Redis
  164. /// </summary>
  165. public void InsertKeyValue(string key, string value)
  166. {
  167. var db = redisConnection.GetDatabase();
  168. db.StringSet(key, value);
  169. }
  170.  
  171. /// <summary>
  172. /// Get a value by key from Redis
  173. /// </summary>
  174. public string GetKeyValue(string key)
  175. {
  176. var db = redisConnection.GetDatabase();
  177. return db.StringGet(key);
  178. }
  179.  
  180. /// <summary>
  181. /// Get and delete a value by key from Redis
  182. /// </summary>
  183. public string GetAndDeleteKeyValue(string key)
  184. {
  185. var db = redisConnection.GetDatabase();
  186. var value = db.StringGet(key);
  187. if (!value.IsNullOrEmpty)
  188. {
  189. db.KeyDelete(key);
  190. }
  191. return value;
  192. }
  193. }
  194. }
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement