134 lines
5.5 KiB
C#
134 lines
5.5 KiB
C#
using CommandLine;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using RedisManager.Utils;
|
|
|
|
namespace RedisManager.Commands
|
|
{
|
|
/// <summary>
|
|
/// Contains command line options and implementations for Redis transaction operations.
|
|
/// Provides functionality for MULTI, EXEC, DISCARD, WATCH, and UNWATCH commands.
|
|
/// </summary>
|
|
[Verb("multi", HelpText = "Mark the start of a transaction block.")]
|
|
public class MultiOptions
|
|
{
|
|
[Option('i', "instance", Required = true, HelpText = "Instance name.")]
|
|
public string Instance { get; set; }
|
|
}
|
|
|
|
[Verb("exec", HelpText = "Execute all commands issued after MULTI.")]
|
|
public class ExecOptions
|
|
{
|
|
[Option('i', "instance", Required = true, HelpText = "Instance name.")]
|
|
public string Instance { get; set; }
|
|
}
|
|
|
|
[Verb("discard", HelpText = "Flush all commands issued after MULTI.")]
|
|
public class DiscardOptions
|
|
{
|
|
[Option('i', "instance", Required = true, HelpText = "Instance name.")]
|
|
public string Instance { get; set; }
|
|
}
|
|
|
|
[Verb("watch", HelpText = "Watch the given keys to determine execution of the MULTI/EXEC block.")]
|
|
public class WatchOptions
|
|
{
|
|
[Option('i', "instance", Required = true, HelpText = "Instance name.")]
|
|
public string Instance { get; set; }
|
|
[Value(0, MetaName = "keys", Min = 1, Required = true, HelpText = "Keys to watch.")]
|
|
public IEnumerable<string> Keys { get; set; }
|
|
}
|
|
|
|
[Verb("unwatch", HelpText = "Forget about all watched keys.")]
|
|
public class UnwatchOptions
|
|
{
|
|
[Option('i', "instance", Required = true, HelpText = "Instance name.")]
|
|
public string Instance { get; set; }
|
|
}
|
|
|
|
public static class TransactionCommands
|
|
{
|
|
/// <summary>
|
|
/// Executes the MULTI command to mark the start of a transaction block.
|
|
/// </summary>
|
|
/// <param name="opts">The MultiOptions containing instance.</param>
|
|
/// <param name="config">The RedisManager configuration.</param>
|
|
/// <returns>Exit code (0 for success, non-zero for failure).</returns>
|
|
public static int RunMulti(MultiOptions opts, Config config)
|
|
{
|
|
var instance = RedisUtils.GetInstance(config, opts.Instance);
|
|
using var redis = RedisUtils.ConnectRedis(instance);
|
|
var db = redis.GetDatabase();
|
|
db.Execute("MULTI");
|
|
Console.WriteLine(Output.Green("OK"));
|
|
return 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes the EXEC command to execute all commands issued after MULTI.
|
|
/// </summary>
|
|
/// <param name="opts">The ExecOptions containing instance.</param>
|
|
/// <param name="config">The RedisManager configuration.</param>
|
|
/// <returns>Exit code (0 for success, non-zero for failure).</returns>
|
|
public static int RunExec(ExecOptions opts, Config config)
|
|
{
|
|
var instance = RedisUtils.GetInstance(config, opts.Instance);
|
|
using var redis = RedisUtils.ConnectRedis(instance);
|
|
var db = redis.GetDatabase();
|
|
var result = db.Execute("EXEC");
|
|
Console.WriteLine(Output.Green(result.ToString()));
|
|
return 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes the DISCARD command to flush all commands issued after MULTI.
|
|
/// </summary>
|
|
/// <param name="opts">The DiscardOptions containing instance.</param>
|
|
/// <param name="config">The RedisManager configuration.</param>
|
|
/// <returns>Exit code (0 for success, non-zero for failure).</returns>
|
|
public static int RunDiscard(DiscardOptions opts, Config config)
|
|
{
|
|
var instance = RedisUtils.GetInstance(config, opts.Instance);
|
|
using var redis = RedisUtils.ConnectRedis(instance);
|
|
var db = redis.GetDatabase();
|
|
db.Execute("DISCARD");
|
|
Console.WriteLine(Output.Green("OK"));
|
|
return 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes the WATCH command to watch the given keys for a transaction block.
|
|
/// </summary>
|
|
/// <param name="opts">The WatchOptions containing instance and keys.</param>
|
|
/// <param name="config">The RedisManager configuration.</param>
|
|
/// <returns>Exit code (0 for success, non-zero for failure).</returns>
|
|
public static int RunWatch(WatchOptions opts, Config config)
|
|
{
|
|
var instance = RedisUtils.GetInstance(config, opts.Instance);
|
|
using var redis = RedisUtils.ConnectRedis(instance);
|
|
var db = redis.GetDatabase();
|
|
var keys = opts.Keys.Select(k => (RedisKey)k).ToArray();
|
|
db.Execute("WATCH", keys);
|
|
Console.WriteLine(Output.Green("OK"));
|
|
return 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes the UNWATCH command to forget about all watched keys.
|
|
/// </summary>
|
|
/// <param name="opts">The UnwatchOptions containing instance.</param>
|
|
/// <param name="config">The RedisManager configuration.</param>
|
|
/// <returns>Exit code (0 for success, non-zero for failure).</returns>
|
|
public static int RunUnwatch(UnwatchOptions opts, Config config)
|
|
{
|
|
var instance = RedisUtils.GetInstance(config, opts.Instance);
|
|
using var redis = RedisUtils.ConnectRedis(instance);
|
|
var db = redis.GetDatabase();
|
|
db.Execute("UNWATCH");
|
|
Console.WriteLine(Output.Green("OK"));
|
|
return 0;
|
|
}
|
|
}
|
|
} |