Redis Windows环境安装
生活随笔
收集整理的這篇文章主要介紹了
Redis Windows环境安装
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、下載Windows 版本 Redis:
? ? https://github.com/ServiceStack/redis-windows
?
2、?解壓文件:
? ??F:\開源代碼學習\01_Redis 打開 目錄:F:\開源代碼學習\01_Redis\src\msopentech\redis64-2.6.12.1
3、啟動Redis
? ? 指向CMD命令:
? ?
4、測試安裝成果:
? 新建一個CMD 窗口:
? 運行命令:
? ??
?
?5、講Redis 加工成 windows :
namespace RedisService {/// <summary>/// 參考文檔:/// http://www.saltwebsites.com/2012/how-run-redis-service-under-windows/// /// sc create Redis start= auto DisplayName= Redis binpath= "\"C:\Program Files\Redis\RedisService.exe\" \"C:\Program Files\Redis\redis.conf\""/// /// </summary>class Program : ServiceBase{const string RedisServer = "redis-server.exe";const string RedisCLI = "redis-cli.exe";static string _path;static int _port;static void Main(string[] args){_path = AppDomain.CurrentDomain.BaseDirectory;if (!File.Exists(Path.Combine(_path, RedisServer)))Exit("Couldn`t find " + RedisServer);if (!File.Exists(Path.Combine(_path, RedisCLI)))Exit("Couldn`t find " + RedisCLI);if (Environment.UserInteractive){SetConsoleCtrlHandler(ConsoleCtrlCheck, true);//Console.CancelKeyPress += (sender, eventArgs) => StopRedis();StartRedis(args.Length == 1 ? args[0] : null);}elseRun(new Program());}protected override void OnStart(string[] args){var arguments = Environment.GetCommandLineArgs();if (arguments.Length > 2)Exit("Too many arguments");base.OnStart(args);StartRedis(arguments.Length == 2 ? arguments[1] : null);}protected override void OnStop(){base.OnStop();StopRedis();}static void StartRedis(string configPath = null){var pi = new ProcessStartInfo(Path.Combine(_path, RedisServer));if (configPath != null){FindPort(configPath);// Workaround for spaces in configuration filename.pi.Arguments = Path.GetFileName(configPath);pi.WorkingDirectory = Path.GetDirectoryName(configPath);}using (var process = new Process { StartInfo = pi }){if (process.Start())if (Environment.UserInteractive)process.WaitForExit();else{}elseExit("Failed to start Redis process");}}private static void FindPort(string path){using (var reader = new StreamReader(path)){string line;while ((line = reader.ReadLine()) != null){if (line.IndexOf("port") == 0){_port = int.Parse(line.Substring(5, line.Length - 5));break;}}if (_port == 0)Exit("Couldn`t find Redis port in config file");}}static void StopRedis(){var pi = new ProcessStartInfo(Path.Combine(_path, RedisCLI)) { Arguments = (_port == 0 ? "" : String.Format("-p {0} ", _port)) + "shutdown" };if (!(new Process { StartInfo = pi }).Start())Exit("Failed to stop Redis process");}static void Exit(string message){if (Environment.UserInteractive){Console.WriteLine(message);Environment.Exit(-1);}else{//File.WriteAllText(Path.Combine(_path, "error.txt"), message);throw new ApplicationException(message);}}[DllImport("Kernel32")]private static extern bool SetConsoleCtrlHandler(HandlerRoutine handler, bool add);// A delegate type to be used as the handler routine // for SetConsoleCtrlHandler.private delegate bool HandlerRoutine(CtrlTypes ctrlType);// An enumerated type for the control messages// sent to the handler routine.private enum CtrlTypes{CTRL_C_EVENT = 0,CTRL_BREAK_EVENT,CTRL_CLOSE_EVENT,CTRL_LOGOFF_EVENT = 5,CTRL_SHUTDOWN_EVENT}private static bool ConsoleCtrlCheck(CtrlTypes ctrlType){StopRedis();return true;}} }?
? ? ?
總結
以上是生活随笔為你收集整理的Redis Windows环境安装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java wait notify
- 下一篇: 开通博客,记录生活