c# 写入 java 操作的 redis 队列
要点:键和值都要序列化或反序列化两次,也就是已经是string后,还要再序列化一次或反序列化一次
namespace RedisDemo { class Program { static void Main(string[] args) { var scannedData = Newtonsoft.Json.JsonConvert.DeserializeObject<ScannedData>(File.ReadAllText(@"ScannedData.json")); scannedData.files[scannedData.files.Keys.ToList()[0]] = new byte[] { }; scannedData.files[scannedData.files.Keys.ToList()[1]] = new byte[] { }; scannedData.processedCount = 0; RedisHelper.Enqueue(Newtonsoft.Json.JsonConvert.SerializeObject("ScannedData"), Newtonsoft.Json.JsonConvert.SerializeObject(Newtonsoft.Json.JsonConvert.SerializeObject(scannedData))); } public class ScannedData { public string schoolUuid { get; set; } public string stjcId { get; set; } public string version { get; set; } public int ifScoreCollate { get; set; } public Dictionary<string, byte[]> files { get; set; } public int processedCount { get; set; } } } }
public static class RedisHelper { private static string RedisConnectionSTR = "127.0.0.1:6379,password=xxxxxx"; private static object RedisLock = new object(); private static ConnectionMultiplexer RedisInstance = null; public static ConnectionMultiplexer Instance { get { if (RedisInstance == null) { lock (RedisLock) { if (RedisInstance == null || RedisInstance.IsConnected == false) { RedisInstance = ConnectionMultiplexer.Connect(RedisConnectionSTR); } } } return RedisInstance; } } public static IDatabase GetRedisDatabase() { return Instance.GetDatabase(); } public static void AddKeyValue(string key, string value) { TimeSpan span = DateTime.Now.AddMinutes(2) - DateTime.Now; GetRedisDatabase().StringSet(key, value, span); } public static string GetKeyValue(string key) { return GetRedisDatabase().StringGet(key); } public static void Enqueue(string key, string value) { GetRedisDatabase().ListLeftPush(key, value); } public static string Dequeue(string key) { return GetRedisDatabase().ListRightPop(key); } }
桂棹兮兰桨,击空明兮溯流光。