Redis

安装Redis

官方网站:http://redis.io/
官方下载:http://redis.io/download 可以根据需要下载不同版本

windows 版 32、64位下载(比较低): https://github.com/dmajkic/redis/downloads 
64位下载:https://github.com/mythz/redis-windows

将Redis设为windows启动项

每次打开命令行启动Redis会很麻烦,把Redis设为windows启动项就不用每次都入命令行了 在redis的目录下新建一个start.bat文件内容为

 F:
 cd F:
edis
 redis-server.exe redis.conf 

再新建一个文件redis_run.vbs内容为

 createobject("wscript.shell").run "F:
edisstart.bat",0

把redis_run.vbs拖到windows启动里运行一下,关闭原来的redis启动cmd窗,在连接窗里输入 get key1 发现OK了,下次开机就会自动启动

windows服务方式安装下载https://github.com/rgl/redis/downloads

http://www.cnblogs.com/mbailing/p/3899965.html

redis-server.exe : 服务程序

redis-check-dump.exe : 本地数据库检查

redis-check-aof.exe : 更新日志检查

redis-benchmark.exe :性能测试,用以模拟同时由N个客户端发送M个 SETs/GETs 查询 (类似于 Apache 的ab 工具).

redis-cli.exe: 服务端开启后,我们的客户端就可以输入各种命令测试了

http://www.tuicool.com/articles/yyYv2aR

首先打开Visual Studio建立一个简单的控制台应用程序,我这里暂时使用的VS2013的版本。然后通过Nuget进行安装Redis常用组件ServiceStack.Redis。

或者引用serviceStack.dll serviceStack.Interfaces.dll serviceStack.ServiceInterfaces.dll这三个库

http://www.aehyok.com/Blog/Detail/98.html

Redis序列号化对象内部已经实现(dotnet底层机制),但在序列化对象导航属性时也会报错,所以在序列号对象时用Josn.net(加导航属性不序列号标记)先序列化为字符串,然后将字符串序列化到Redis,反序列化为字符串,然后通过JOSN.NET反序列化为对象

或者通过Redis hash存储(将对象中的属性一一存入)

解决日志文件并发的问题

 public class MyExceptionAttribute : HandleErrorAttribute
    {
       // public static Queue<Exception> ExceptionQueue = new Queue<Exception>();

        public static IRedisClientsManager clientManager = new PooledRedisClientManager(new string[] { "127.0.0.1:6379","192.168.1.2:6379"});
        public static IRedisClient redisClent = clientManager.GetClient();


        public override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
          //  ExceptionQueue.Enqueue(filterContext.Exception);//将异常信息添加到队列中。
            redisClent.EnqueueItemOnList("errorMsg", filterContext.Exception.ToString());

            filterContext.HttpContext.Response.Redirect("/Error.html");

        }
    }

在Global.asax对队列进行文件写数据

 public class MvcApplication : SpringMvcApplication //System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            SearchIndexManager.GetInstance().StartThread();//开启线程扫描队列将数据取出来写到Lucene.NET中。
            log4net.Config.XmlConfigurator.Configure();//读取Log4Net配置信息
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            string fileLogPath = Server.MapPath("/Log/");
            //WaitCallback
            ThreadPool.QueueUserWorkItem((a) =>
            {
                while (true)
                {
                    //if (MyExceptionAttribute.ExceptionQueue.Count > 0)
                    if (MyExceptionAttribute.redisClent.GetListCount("errorMsg")>0)
                    {
                      // Exception ex= MyExceptionAttribute.ExceptionQueue.Dequeue();//出队

                        string ex = MyExceptionAttribute.redisClent.DequeueItemFromList("errorMsg");

                       //string fileName = DateTime.Now.ToString("yyyy-MM-dd")+".txt";
                       //File.AppendAllText(fileLogPath + fileName, ex.ToString(), System.Text.Encoding.Default);
                       ILog logger = LogManager.GetLogger("errorMsg");
                       logger.Error(ex);
                    }
                    else
                    {
                        Thread.Sleep(3000);//如果队列中没有数据,休息避免造成CPU的空转.
                    }
                }


            },fileLogPath);
        }

可以在Redis服务器上进行队列读取,并写日志文件,以减轻WEB服务器的压力。WEB服务器只是捕获异常,并写入队列。

原文地址:https://www.cnblogs.com/ecollab/p/6163187.html