//max memory to use for memcached items in megabytes, default is 64 MB public MemCache(int memorySize) { //the goal is copy memcached.exe to %windir% folder, then from there, install memcached windows service so that the executable path of the service is %windir%\memcached.exe. Thus, PBS folder could be moved to anywhere else. string strCmdResult; try { //check if memcached.exe,msvcr71.dll exists in PBS folder if (!File.Exists("memcached.exe")) { throw new Exception("memcached.exe doesn't exists!"); } if (!File.Exists("msvcr71.dll")) { throw new Exception("msvcr71.dll doesn't exists!"); } _memcachedInWinFolder = Environment.ExpandEnvironmentVariables("%SystemRoot%") + "\\memcached.exe"; //check if memcached.exe,pthreadGC2.dll exists in windows path if (!File.Exists(_memcachedInWinFolder)) { File.Copy("memcached.exe", _memcachedInWinFolder, true); } if (!File.Exists(Environment.ExpandEnvironmentVariables("%SystemRoot%") + "\\msvcr71.dll")) { File.Copy("msvcr71.dll", Environment.ExpandEnvironmentVariables("%SystemRoot%") + "\\msvcr71.dll", true); } //if windows service exists, check if the exe path of the service is in windows directory if (Utility.IsWindowsServiceExisted("memcached Server")) { Utility.Cmd("sc qc \"memcached Server\"", false, out strCmdResult); if (!strCmdResult.Contains("\\Windows\\")) { Utility.Cmd("sc delete \"memcached Server\"", false, out strCmdResult);//try to uninstall windows service } } //check if windows service exists if (!Utility.IsWindowsServiceExisted("memcached Server")) { Utility.Cmd(_memcachedInWinFolder + " -d install", false, out strCmdResult); //install memcached windows service //google:使用cmd命令手动、自动启动和禁用服务 Utility.Cmd("sc config \"memcached Server\" start= demand", false, out strCmdResult); //set to 手动启动 } Utility.Cmd(_memcachedInWinFolder + " -d stop", false, out strCmdResult); _cmdMemcachedProcess = Utility.Cmd(_memcachedInWinFolder + " -m " + memorySize + " -d start", true, out strCmdResult); string[] serverlist = { "127.0.0.1:11211" }; // initialize the pool for memcache servers _pool = SockIOPool.GetInstance(); _pool.SetServers(serverlist); _pool.InitConnections = 3; _pool.MinConnections = 3; _pool.MaxConnections = 5; _pool.SocketConnectTimeout = 1000; _pool.SocketTimeout = 3000; _pool.MaintenanceSleep = 30; _pool.Failover = true; _pool.Nagle = false; _pool.Initialize(); if (_pool.GetConnection(serverlist[0]) == null) { _pool.Shutdown(); Utility.Cmd("sc delete \"memcached Server\"", false, out strCmdResult);//try to uninstall windows service throw new Exception("Can not managed to run 'memcached -d start' on your machine."); } MC = new MemcachedClient() { EnableCompression = false }; //try to cache one object in memory to ensure memcached ability can be used if (!MC.Set("test", DateTime.Now.ToString())) { throw new Exception("Can't managed to set key-value in memched!"); } IsActived = true; } catch (Exception e) { IsActived = false; Shutdown();//important //copy error:"Access to the path 'C:\Windows\system32\memcached.exe' is denied." if (e.Message.Contains("Access") && e.Message.Contains("denied")) { throw new Exception("Copy memcached.exe to '%windir%' failed.\r\nPlease reopen PortableBasemapServer by right clicking and select 'Run as Administrator'."); } throw new Exception(e.Message); } }