zoukankan      html  css  js  c++  java
  • c#缓存 笔记

    1:缓存。

    你需要了解大数据高并发的瓶颈在哪里,一般都是数据库层面的,机械硬盘承载不起非常快速的读写操作,cpu承载不起大量的逻辑运算,所以最基本的解决思路就是:
    1.换固态硬盘加快硬盘的读写效率。
    2.建立缓存中间件降低对硬盘的读写次数,缓存不用多说了,最最最基本和重要的优化策略。
    3.将硬盘的读写或者数据的计算分摊到多台机器上,也就是集群。hadoop就是基于这个层面的。
    4.良好的查询算法,降低读的次数,分表,分库,索引等都是基于这层面的。

    理论上来讲,在带宽充裕的情况下,只要遵循上面的4个思路进行延伸就可以解决大部分的高并发问题。

    1。整页缓存

    <%@ OutputCache Duration="20" VaryByParam="none" %>
    Duration 设置缓存到期时间,单位:秒。
    VaryByParam:可用来使缓存输出因查询字符串而异,多个查询字符用分号隔开

    1.2 当使用整页缓存时,可以根据页面url的不同参数来设置不同的 页面缓存 版本。

    <%@ OutputCache  Duration="20" VaryByParam="id" %>

    mvc对应 原理是利用特性

     [OutputCache(Duration=10000)]

    2 数据源缓存

      <asp:ObjectDataSource EnableCaching="true" CacheDuration="20" ID="ObjectDataSource1" runat="server" SelectMethod="GetList" TypeName="BLL.Classes"></asp:ObjectDataSource>

    其中getList是方法 不过这个方式在企业一般不推荐 。至少个人一般不用控件

    3:自定义缓存

    List<MODEL.Classes> list=null;
    
                if (Cache["myDog"] == null)
                {
                    Cache["myDog"] = new Dog() { StrName = "小瑞瑞", StrType = "柯基犬" };
                    Response.Write("保存了一只狗狗");
                    //查询数据库 存入缓存
                    list = new BLL.Classes().GetList();
                    Cache["list"] =  list;
                }
                else
                {
                    Dog myDog = Cache["myDog"] as Dog;
                    Response.Write("myDog="+myDog.StrName);
                    //从缓存里获取数据
                    list = Cache["list"] as List<MODEL.Classes>;
                }
               myDog, list 就可以使用了

    4.文件依赖项自定义缓存(新建一个文件 这里是txt)

                List<MODEL.Classes> list=null;
    
                if (Cache["list"] == null)
                {
                    //查询数据库 存入缓存
                    list = new BLL.Classes().GetList();
                    //设置绝对过期时间
                    //Cache.Insert("list", list, null, DateTime.Now.AddSeconds(20),System.Web.Caching.Cache.NoSlidingExpiration);
                    //设置滑动过期时间
                    //Cache.Insert("list", list, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(10000));
                    //带 文件缓存依赖
                    string strPath = Server.MapPath("1.txt");
                    //文件缓存依赖项
                    System.Web.Caching.CacheDependency fileDep = new System.Web.Caching.CacheDependency(strPath);
                    //创建带文件依赖的缓存 键值项
                    Cache.Insert("list", list, fileDep, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration);
    
                    Response.Write("依赖文件被修改了,又重新读取数据列表存入缓存中了~~!");
                    
                }
                else
                {
                    //从缓存里获取数据
                    list = Cache["list"] as List<MODEL.Classes>;
                }
    使用缓存list

    5:数据库缓存依赖 

    借用博客园的知识库

    http://kb.cnblogs.com/page/69727/

     10进制转换 16进制 

    int str = 20;
    Console.WriteLine(str.ToString("X"));

    c# 判断字符串是否包含数字

            static void Main(string[] args)
            {
                string str1 = "111a1";
                var sss= IsInteger(str1);
                Console.WriteLine(sss); ;
                Console.Read();
    
            }
    
    
    
            public static bool IsInteger(string s)
            {
                string pattern = @"^d*$";
                return Regex.IsMatch(s, pattern);
            }

     c#判断字符串数字 并取出

                string test = "方法.1";
                int count = Regex.Matches(test, @"d").Count;
                Console.WriteLine("字符串中数字的个数为:{0}", count);
                Console.WriteLine("连续整数分别为:");
                MatchCollection mc = Regex.Matches(test, @"d+");
                foreach (Match m in mc)
                {
                    Console.WriteLine(m.Value);
                }

    异或效验:

            public static string ConvertHexToXor2(string hex)
            {
                string xor = "";
                for (int i = 0; i < hex.Length; i += 2)
                {
                    string str = hex.Substring(i, 2);
                    if (xor != "")
                    {
                        xor = (Convert.ToInt64(xor, 16) ^ Convert.ToInt64(str, 16)).ToString("X2");
                    }
                    else
                    {
                        xor = str;
                    }
                }
                return xor;
            }

    优化修改
                //写校验码
                stream.Position = 5;
                byte value = (byte)stream.ReadByte();
                while (stream.Position != stream.Length - 2)
                {
                    value = (byte)(value ^ (byte)stream.ReadByte());  //校验码计算
                }
                stream.Position = stream.Length - 2; //设置流在 验证码开始位置
                stream.WriteByte(value);  //写验证码
    
    
    

      

     
            private DataView FillData(DataTable dt)
            {
                if (dt == null || dt.Rows.Count == 0)
                {
                    return null;
                }
                #region ---此列groupTime已在数据库添加
                //dt.Columns.Add("groupTime", typeof(DateTime));
                //foreach (DataRow dr in dt.Rows)
                //{
                //    dr["groupTime"] = DateTime.Parse(dr["Dyn_Time"].ToString()).ToString("yyyy-MM-dd");
                //} 
                #region  tempTimeSort 对时间排序否则每次运算结果有细微的变化 此方法等价于string.Join("#", m.Select(x => x.Field<DateTime>("Dyn_Time")).OrderBy(x => x)) 优化
                //List<DateTime> tempTimeSort = null;
                //tempTimeSort = new List<DateTime>();
                //for (int k = 0; k < tempTimes.Length; k++)
                //{
                //    tempTimeSort.Add(DateTime.Parse(tempTimes[k]));
                //}
                //tempTimeSort.Sort(); 
                #endregion
                #endregion
    
                #region 添加列分组 并统计运算
                var dataList = from item in dt.AsEnumerable().AsParallel()
                               group item by new
                               {
                                   dyn_vehid = item.Field<int>("dyn_vehid"),
                                   groupTime = item.Field<string>("groupTime")
                               } into m
                               where m.Count() > 1
                               orderby m.Key.dyn_vehid descending
                               select new
                               {
                                   dyn_vehid = m.Key.dyn_vehid,
                                   dyn_Time = string.Join("#", m.Select(x => x.Field<DateTime>("Dyn_Time")).OrderBy(x => x)),
                                   groupTime = m.Key.groupTime,
                                   v_companyid = m.First().Field<int>("v_companyid")
                               };
    
                var obcVhicle = new List<ClsVehicle>();
                int sumSecond = 0;
                foreach (var item in dataList)
                {
                    var tempTimes = item.dyn_Time.Split(new string[] { "#" }, StringSplitOptions.RemoveEmptyEntries);
                    for (int w = 0; w < tempTimes.Length - 1; w++)
                    {
                        var secondTemp = "".TimeSeconds(tempTimes[w], tempTimes[w + 1]);
                        if (secondTemp < TxtdevicesInterval)
                        {
                            sumSecond += secondTemp;
                        }
                    }
                    double sums = double.Parse(sumSecond.ToString()) / 86400;
                    var v_noTemp = VehList.Where(y => y.iVId == item.dyn_vehid).ToList();
                    var vehicle = new ClsVehicle()
                    {
                        v_no = v_noTemp[0].strVehNum,
                        dyn_vehid = item.dyn_vehid,
                        C_EnterpriseName = Company.Where(x => x.CompanyId == item.v_companyid).ToList()[0].CompanyName,
                        Dyn_Time = (sums * 100).ToString("F3") + "%",
                        V_LicenseColor = v_noTemp[0].strVehNumColor,
                        groupTime = item.groupTime
                    };
                    sumSecond = 0;
                    obcVhicle.Add(vehicle);
                }
                #endregion
    
                #region 数据源行合并
                foreach (var g in obcVhicle.AsParallel().GroupBy(x => x.dyn_vehid))
                {
                    var dataFist = g.First();
                    if (g.Count() > 1)
                    {
                        tempDt.Rows.Add(dataFist.C_EnterpriseName, dataFist.v_no, dataFist.V_LicenseColor, string.Join("#", g.Select(x => x.groupTime + "$" + x.Dyn_Time)), "", "", dataFist.dyn_vehid);
                    }
                    else
                    {
                        tempDt.Rows.Add(dataFist.C_EnterpriseName, dataFist.v_no, dataFist.V_LicenseColor, "", dataFist.groupTime, dataFist.Dyn_Time, dataFist.dyn_vehid);
                    }
                }
                #endregion
    
                #region 数据源行转换列有bug RemoveAt后索引改变 应该申明临时变量保存
                //var isOne = false;
                //for (int i = 0; i < obcVhicle.Count; i++)
                //{
                //    for (int j = i + 1; j < obcVhicle.Count; j++)
                //    {
                //        if (obcVhicle[i].dyn_vehid == obcVhicle[j].dyn_vehid)
                //        {
                //            if (!isOne)
                //            {
                //                isOne = true;
                //                obcVhicle[i].groupTime += "#" + obcVhicle[i].Dyn_Time + "$" + obcVhicle[j].groupTime + "#" + obcVhicle[j].Dyn_Time;
                //            }
                //            else
                //            {
                //                obcVhicle[i].groupTime += "$" + obcVhicle[j].groupTime + "#" + obcVhicle[j].Dyn_Time;
                //            }
                //            obcVhicle.RemoveAt(j); ;
                //        }
                //    }
                //    isOne = false;
                //} 
                #endregion
    
                #region 构建dtList 并赋值
                if (resultDtList == null || tempDt == null)
                {
                    Thread.Sleep(1000);
                }
                for (int k = 0; k < tempDt.Rows.Count; k++)
                {
                    DataRow row = resultDtList.NewRow();
                    row[0] = tempDt.Rows[k][0];
                    row[1] = tempDt.Rows[k][1];
                    row[2] = tempDt.Rows[k][2];
                    row[3] = int.Parse(tempDt.Rows[k][6].ToString());
                    string[] strValues =null;
                    var tempArr = tempDt.Rows[k]["result"].ToString();
                    if (!string.IsNullOrEmpty(tempArr))
                    {
                        strValues = tempArr.Split('#');
                    }
                    //!= null说明有多条
                    if (strValues != null)
                    {
                        for (int sp = 0; sp < strValues.Length; sp++)
                        {
                            var strValue = strValues[sp].Split('$');
                            if (strValue.Length == 2)
                            {
                                row[strValue[0] + "在线率"] = strValue[1];
                            }
                        }
                        resultDtList.Rows.Add(row);
                    }
                    else
                    {   //只有一条数据
                        var tempClomn = tempDt.Rows[k][4];
                        row[tempClomn + "在线率"] = tempDt.Rows[k][5];
                        resultDtList.Rows.Add(row);
                    }
                }
                #endregion
    
                #region 处理数据源 dtList排序注意dyn_vehid为lnt类型 dt没空的默认赋值0
                DataView dv = resultDtList.DefaultView;
                dv.Sort = "dyn_vehid DESC";
    
                DataTable dtTemp = dv.ToTable();
                dtTemp.Columns.Remove("dyn_vehid");
    
                int cols = dtTemp.Columns.Count;
                int rows = dtTemp.Rows.Count;
                for (int xp = 0; xp < rows; xp++)
                {
                    for (int xps = 3; xps < cols; xps++)
                    {
                        if (string.IsNullOrEmpty(dtTemp.Rows[xp][xps].ToString()) || dtTemp.Rows[xp][xps] == null)
                        {
                            dtTemp.Rows[xp][xps] = "0";
                        }
                    }
                }
                #endregion
                return dtTemp.DefaultView;
            }

     一般处理程序获取post参数失败

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using SMS.Common;
    
    namespace SMS.FileUpload
    {
        /// <summary>
        /// FileUpload 的摘要说明
        /// </summary>
        public class FileUpload : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.Buffer = true;
                context.Response.ExpiresAbsolute = DateTime.Now.AddSeconds(0);
                context.Response.AddHeader("Pragma", "No-Cache");
                context.Response.Expires = 0;
                //context.Response.ContentType = "text/plain";
                //context.Response.Charset = "utf-8";
    
                string action = context.Request["action"];                          //获取操作类型
                switch (action)
                {
                    default: ResponseFileUpload(context); break;
                }
            }
    
    
            private void ResponseFileUpload(HttpContext context)
            {
                var path = "";
                var fileName = "";
                FileStream fstream = null;
                try
                {
                    StreamReader stw = new StreamReader(HttpContext.Current.Request.InputStream);
                    string PostData = stw.ReadToEnd();
    
                    var p = DynamicJson.Parse(PostData.ToString());
    
                    var content = p.Content;
                  //  fileName = p.Name;
    
                    //var content =context.Request["Content"];
                    //fileName = context.Request["Name"];
                    string[] Extension = p.Name.Split('.');
    
                    fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + Extension[1];
    
                    path = HttpContext.Current.Server.MapPath(string.Format("../UserFile/{0}", DateTime.Now.ToString("yyyyMMdd")));
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    if (content!=null)
                    {
                        byte[] imageBytes = Convert.FromBase64String(content);
                        fstream = File.Create(path +"/"+ fileName, imageBytes.Length);
                        fstream.Write(imageBytes, 0, imageBytes.Length);
                        fstream.Close();
                    }
                    ResponseMsg(context, string.Format("UserFile/{0}/{1}", DateTime.Now.ToString("yyyyMMdd"), fileName));
                }
                catch 
                {
                    context.Response.Write("{ "result":-1}");
                    fstream.Close();
                }
            }
    
            public void ResponseMsg(HttpContext context, string path)
            {
                context.Response.Write("{ "result":"" + path + ""}");
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
  • 相关阅读:
    nginx-1.8.1的安装
    ElasticSearch 在3节点集群的启动
    The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
    sqoop导入导出对mysql再带数据库test能跑通用户自己建立的数据库则不行
    LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)
    LeetCode 437. Path Sum III (路径之和之三)
    LeetCode 404. Sum of Left Leaves (左子叶之和)
    LeetCode 257. Binary Tree Paths (二叉树路径)
    LeetCode Questions List (LeetCode 问题列表)- Java Solutions
    LeetCode 561. Array Partition I (数组分隔之一)
  • 原文地址:https://www.cnblogs.com/y112102/p/3281750.html
Copyright © 2011-2022 走看看