Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.IO;
- using System.Text.RegularExpressions;
- using ZennoLab.CommandCenter;
- using ZennoLab.InterfacesLibrary;
- using ZennoLab.InterfacesLibrary.ProjectModel;
- using ZennoLab.InterfacesLibrary.ProjectModel.Collections;
- using ZennoLab.InterfacesLibrary.ProjectModel.Enums;
- using ZennoLab.Macros;
- using Global.ZennoExtensions;
- using ZennoLab.Emulation;
- using StackExchange.Redis;
- namespace ZennoLab.OwnCode
- {
- /// <summary>
- /// A simple class of the common code
- /// </summary>
- public class CommonCode
- {
- /// <summary>
- /// Lock this object to mark part of code for single thread execution
- /// </summary>
- public static object SyncObject = new object();
- // Insert your code here
- }
- public class RedisDB
- {
- private string connectionString;
- private ConnectionMultiplexer redisConnection;
- public RedisDB(string connectionString)
- {
- this.connectionString = connectionString;
- redisConnection = ConnectionMultiplexer.Connect(connectionString);
- }
- public void Open()
- {
- if (redisConnection == null || !redisConnection.IsConnected)
- {
- redisConnection = ConnectionMultiplexer.Connect(connectionString);
- }
- }
- public void Close()
- {
- if (redisConnection != null && redisConnection.IsConnected)
- {
- redisConnection.Dispose();
- redisConnection = null;
- }
- }
- /// <summary>
- /// Sync a ZennoPoster list to Redis as a list, replacing old data
- /// </summary>
- /// <param name="redisKey">The Redis list key</param>
- /// <param name="project">ZennoPoster project instance</param>
- public void SyncZennoListToRedis(string redisKey, IZennoPosterProjectModel project)
- {
- var db = redisConnection.GetDatabase();
- // Clear the current Redis list
- db.KeyDelete(redisKey);
- // Add items from ZennoPoster list to Redis
- var zennoList = project.Lists["List1"];
- foreach (var item in zennoList)
- {
- db.ListRightPush(redisKey, item);
- }
- }
- /// <summary>
- /// Add ZennoPoster list items to Redis without removing old data
- /// </summary>
- /// <param name="redisKey">The Redis list key</param>
- /// <param name="project">ZennoPoster project instance</param>
- public void AppendZennoListToRedis(string redisKey, IZennoPosterProjectModel project)
- {
- var db = redisConnection.GetDatabase();
- // Add items from ZennoPoster list to Redis without deleting old data
- var zennoList = project.Lists["List1"];
- foreach (var item in zennoList)
- {
- db.ListRightPush(redisKey, item);
- }
- }
- /// <summary>
- /// Delete a Redis list by key
- /// </summary>
- /// <param name="redisKey">The Redis list key to delete</param>
- public void DeleteRedisList(string redisKey)
- {
- var db = redisConnection.GetDatabase();
- bool isDeleted = db.KeyDelete(redisKey);
- if (isDeleted)
- {
- Console.WriteLine($"Redis list with key '{redisKey}' has been successfully deleted.");
- }
- else
- {
- Console.WriteLine($"Redis list with key '{redisKey}' does not exist or could not be deleted.");
- }
- }
- /// <summary>
- /// Pop a string from Redis list (remove and return it)
- /// </summary>
- /// <param name="redisKey">The Redis list key</param>
- /// <returns>String popped from the Redis list</returns>
- public string PopFromRedisList(string redisKey)
- {
- var db = redisConnection.GetDatabase();
- return db.ListLeftPop(redisKey);
- }
- /// <summary>
- /// Insert a list of values into Redis
- /// </summary>
- /// <param name="key">Redis key</param>
- /// <param name="values">List of values to insert</param>
- public void InsertList(string key, List<string> values)
- {
- var db = redisConnection.GetDatabase();
- foreach (var value in values)
- {
- db.ListRightPush(key, value);
- }
- }
- /// <summary>
- /// Get all values from a Redis list
- /// </summary>
- /// <param name="key">Redis key</param>
- /// <returns>List of strings from Redis</returns>
- public List<string> GetList(string key)
- {
- var db = redisConnection.GetDatabase();
- var length = db.ListLength(key);
- var list = db.ListRange(key, 0, length - 1);
- var result = new List<string>();
- foreach (var item in list)
- {
- result.Add(item.ToString());
- }
- return result;
- }
- /// <summary>
- /// Insert a single key-value pair into Redis
- /// </summary>
- public void InsertKeyValue(string key, string value)
- {
- var db = redisConnection.GetDatabase();
- db.StringSet(key, value);
- }
- /// <summary>
- /// Get a value by key from Redis
- /// </summary>
- public string GetKeyValue(string key)
- {
- var db = redisConnection.GetDatabase();
- return db.StringGet(key);
- }
- /// <summary>
- /// Get and delete a value by key from Redis
- /// </summary>
- public string GetAndDeleteKeyValue(string key)
- {
- var db = redisConnection.GetDatabase();
- var value = db.StringGet(key);
- if (!value.IsNullOrEmpty)
- {
- db.KeyDelete(key);
- }
- return value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement