c#缓存 笔记

1:缓存。

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

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

1。整页缓存

<%@ OutputCache Duration="" VaryByParam="none" %>
Duration 设置缓存到期时间,单位:秒。
VaryByParam:可用来使缓存输出因查询字符串而异,多个查询字符用分号隔开。 1.2 当使用整页缓存时,可以根据页面url的不同参数来设置不同的 页面缓存 版本。
<%@ OutputCache  Duration="" VaryByParam="id" %>

mvc对应 原理是利用特性

 [OutputCache(Duration=)]

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 = ; i < hex.Length; i += )
{
string str = hex.Substring(i, );
if (xor != "")
{
xor = (Convert.ToInt64(xor, ) ^ Convert.ToInt64(str, )).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 == )
{
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() >
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 = ;
foreach (var item in dataList)
{
var tempTimes = item.dyn_Time.Split(new string[] { "#" }, StringSplitOptions.RemoveEmptyEntries);
for (int w = ; w < tempTimes.Length - ; w++)
{
var secondTemp = "".TimeSeconds(tempTimes[w], tempTimes[w + ]);
if (secondTemp < TxtdevicesInterval)
{
sumSecond += secondTemp;
}
}
double sums = double.Parse(sumSecond.ToString()) / ;
var v_noTemp = VehList.Where(y => y.iVId == item.dyn_vehid).ToList();
var vehicle = new ClsVehicle()
{
v_no = v_noTemp[].strVehNum,
dyn_vehid = item.dyn_vehid,
C_EnterpriseName = Company.Where(x => x.CompanyId == item.v_companyid).ToList()[].CompanyName,
Dyn_Time = (sums * ).ToString("F3") + "%",
V_LicenseColor = v_noTemp[].strVehNumColor,
groupTime = item.groupTime
};
sumSecond = ;
obcVhicle.Add(vehicle);
}
#endregion #region 数据源行合并
foreach (var g in obcVhicle.AsParallel().GroupBy(x => x.dyn_vehid))
{
var dataFist = g.First();
if (g.Count() > )
{
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();
}
for (int k = ; k < tempDt.Rows.Count; k++)
{
DataRow row = resultDtList.NewRow();
row[] = tempDt.Rows[k][];
row[] = tempDt.Rows[k][];
row[] = tempDt.Rows[k][];
row[] = int.Parse(tempDt.Rows[k][].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 = ; sp < strValues.Length; sp++)
{
var strValue = strValues[sp].Split('$');
if (strValue.Length == )
{
row[strValue[] + "在线率"] = strValue[];
}
}
resultDtList.Rows.Add(row);
}
else
{ //只有一条数据
var tempClomn = tempDt.Rows[k][];
row[tempClomn + "在线率"] = tempDt.Rows[k][];
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 = ; xp < rows; xp++)
{
for (int xps = ; xps < cols; xps++)
{
if (string.IsNullOrEmpty(dtTemp.Rows[xp][xps].ToString()) || dtTemp.Rows[xp][xps] == null)
{
dtTemp.Rows[xp][xps] = "";
}
}
}
#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();
context.Response.AddHeader("Pragma", "No-Cache");
context.Response.Expires = ;
//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[]; 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, , 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;
}
}
}
}
上一篇:数据分页处理系列之二:HBase表数据分页处理


下一篇:Chrome必备的扩展