一、安装Memcached

Memcached1.2.6

http://files.cnblogs.com/files/jasonduan/11465401756756.zip

Memcached.ClientLibrary

http://files.cnblogs.com/files/jasonduan/10524626586159.zip

网上好多文章

http://jingyan.baidu.com/article/335530da5f765019cb41c3ec.html

http://jingyan.baidu.com/album/c85b7a640fbfd5003bac9500.html

memcached.exe -d install

memcached.exe -d start

Memcached还有其他的一些常用的命令如下:

-p 监听的端口 --默认端口11211
      -l 连接的IP地址, 默认是本机
      -d start 启动memcached服务
      -d restart 重起memcached服务
      -d stop|shutdown 关闭正在运行的memcached服务
      -d install 安装memcached服务
      -d uninstall 卸载memcached服务
      -u 以的身份运行 (仅在以root运行的时候有效)
      -m 最大内存使用,单位MB。默认64MB
      -M 内存耗尽时返回错误,而不是删除项 默认64
      -c 最大同时连接数,默认是1024
      -f 块大小增长因子,默认是1.25
      -n 最小分配空间,key+value+flags默认是48
      -h 显示帮助

在运行下输入“regedit”打开注册表,  找到路径 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached,下面找 到一个ImagePath 的字符串项,正好是服务的执行路径的字符串,

双击该串,在后面追加入“-m 1024 -c 2048 -p 11200”  重启服务即可

查看缓存区块,分析结果可调配-f参数
telnet 127.0.0.1 11200

查看缓存的各种状态

stats

二、定义MemCachedHelper

<add key="memcachedServer" value="127.0.0.1:11200" />

using System.Linq;
using System.Web;
using System.Configuration;
using Memcached.ClientLibrary;
using System;
using System.Collections; namespace API.Common
{
public static class MemCachedHelper
{
private static readonly MemcachedClient memcachedClient; static MemCachedHelper()
{
//读取web.config中的memcached服务器配置信息
if (string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["memcachedServer"]))
{
throw new Exception("请在web.config的appsetting中配置memcached服务器信息!");
}
string[] servers = ConfigurationManager.AppSettings["memcachedServer"].Split(new char[','], StringSplitOptions.RemoveEmptyEntries); //初始化池
SockIOPool pool = SockIOPool.GetInstance();
pool.SetServers(servers);
pool.InitConnections = 3;
pool.MinConnections = 3;
pool.MaxConnections = 1000;
pool.SocketConnectTimeout = 1000;
pool.SocketTimeout = 3000;
pool.MaintenanceSleep = 30;
pool.Failover = true;
pool.Nagle = false;
pool.Initialize();
memcachedClient = new MemcachedClient();
memcachedClient.EnableCompression = false;
} /// <summary>
/// 根据key获取value
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static object Get(string key)
{
return memcachedClient.Get(key);
}
public static T Get<T>(string key)
{
return (T)memcachedClient.Get(key);
} public static Hashtable Get(string[] key)
{
return memcachedClient.GetMultiple(key);
} public static bool Set(string key, object objObject, DateTime exp)
{
return memcachedClient.Set(key, objObject, exp);
}
public static bool Set(string key, object value)
{
return memcachedClient.Set(key, value);
}
public static bool Set(string key, object value, int minute)
{
return memcachedClient.Set(key, value, DateTime.Now.AddMinutes(minute));
} public static void Set<T>(string key, T values)
{
memcachedClient.EnableCompression = false;
memcachedClient.Set(key, values);
}
public static void Set<T>(string key, T values, DateTime expiry)
{
try
{
memcachedClient.EnableCompression = false;
memcachedClient.Set(key, values, expiry);
}
catch
{
}
} public static Hashtable Stats()
{
return memcachedClient.Stats();
} public static void Remove(string key)
{
memcachedClient.Delete(key);
} public static void RemoveAllCache()
{
memcachedClient.FlushAll();
} public static bool ContainsKey(string key)
{
return memcachedClient.KeyExists(key);
}
public static bool IsKeyExists(string key)
{
try
{
return memcachedClient.KeyExists(key);
}
catch
{
return false;
}
}
public static bool Replace<T>(string key, T values, DateTime expiry)
{
return memcachedClient.Replace(key, values, expiry);
}
public static bool Replace<T>(string key, T values)
{
return memcachedClient.Replace(key, values);
}
}
}

三使用

using API.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using C = API.Common;
using B = API.BLL;
using M = API.Model;
namespace API.BPJ.Controllers.Product
{
/// <summary>
/// 商品控制器
/// </summary>
public class ProductController : ApiController
{ /// <summary>
/// 根据经销商编号得到分类列表
/// </summary>
/// <param name="UserId"></param>
/// <param name="ZY"></param>
/// <param name="Sign"></param>
/// <param name="Ts"></param>
/// <returns></returns>
[HttpGet]
public CommonResult GetProductCategoryList(string UserId, string ZY, string Sign, string Ts)
{
CommonResult apiResult = new CommonResult();
//检查请求 签名和时间戳不符合即返回
if (!C.ComHelper.CheckRequest(Sign, Ts, out apiResult.Result, out apiResult.Message))
{
return apiResult;
}
string cachekey = "CategoryList" + UserId + ZY;
if (!C.MemCachedHelper.IsKeyExists(cachekey))
{
//如果没有缓存从数据库读取 默认设置缓存为1天时间
List<M.ProductCategory> list = new B.Product().GetProductCategoryList(UserId, ZY);
C.MemCachedHelper.Set<List<M.ProductCategory>>(cachekey, list, DateTime.Now.AddDays(1));
apiResult.Data = list; }
else
{
apiResult.Data = C.MemCachedHelper.Get<List<M.ProductCategory>>(cachekey);
} if (apiResult.Data != null)
{
apiResult.Result = "1";
apiResult.Message = "加载成功!"; }
else
{
apiResult.Result = "2";
apiResult.Message = "加载失败!";
}
return apiResult;
} } }

四注意问题

1、model需要Serializable 否则不能set

2、序列化后的json对象,每个属性都带有k__BackingField后缀,加入[DataContract]  [DataMember]就就可以了

namespace API.Model
{ [Serializable]
[DataContract]
public class ProductCategory
{
[DataMember]
public int CId { get; set; }
[DataMember]
public string CName { get; set; }
[DataMember]
public string CCode { get; set; }
[DataMember]
public string PCode { get; set; }
[DataMember]
public string Thumbnail { get; set; }
[DataMember]
public string Picture { get; set; } }
}

localhost:6103/api/Product/GetProductCategoryList?UserId=1&ZY=1&Sign=&Ts=

{
"Result": "1",
"Message": "加载成功!",
"Data": [
{
"CId": 268,
"CName": "个护化妆",
"CCode": "003",
"PCode": "0",
"Thumbnail": "http://images.bpj.com/ProductCategory/201603/gehuhuazhang.jpg",
"Picture": "http://images.bpj.com/ProductCategory/201603/gehuhuazhang.jpg"
},
{
"CId": 269,
"CName": "个护健康",
"CCode": "004",
"PCode": "0",
"Thumbnail": "http://images.bpj.com/ProductCategory/201603/gehujiankang.jpg",
"Picture": "http://images.bpj.com/ProductCategory/201603/gehujiankang.jpg"
},
{
"CId": 338,
"CName": "家居用品",
"CCode": "005",
"PCode": "0",
"Thumbnail": "http://images.bpj.com/ProductCategory/201603/jiajuyongpin.jpg",
"Picture": "http://images.bpj.com/ProductCategory/201603/jiajuyongpin.jpg"
},
{
"CId": 499,
"CName": "母婴玩具",
"CCode": "007",
"PCode": "0",
"Thumbnail": "http://images.bpj.com/ProductCategory/201603/muyingwanju.jpg",
"Picture": "http://images.bpj.com/ProductCategory/201603/muyingwanju.jpg"
}
]
}

Memcached+WebApi记录的更多相关文章

  1. centos7下安装php+memcached简单记录

    1)centos7下安装php 需要再添加一个yum源来安装php-fpm,可以使用webtatic(这个yum源对国内网络来说恐怕有些慢,当然你也可以选择其它的yum源) [root@nextclo ...

  2. Memcached简介

    在Web服务开发中,服务端缓存是服务实现中所常常采用的一种提高服务性能的方法.其通过记录某部分计算结果来尝试避免再次执行得到该结果所需要的复杂计算,从而提高了服务的运行效率. 除了能够提高服务的运行效 ...

  3. memcached 常用命令及使用说明

    1.启动Memcache 常用参数 -p <num> 设置TCP端口号(默认设置为: ) -U <num> UDP监听端口(默认: , 时关闭) -l <ip_addr& ...

  4. Memcached常用命令及使用说明(转)

    一.存储命令 存储命令的格式: 1 2 <command name> <key> <flags> <exptime> <bytes> < ...

  5. Windows和Linux环境下Memcached安装与配置(转)

    一.memcached安装配置 windows平台安装 1.memcached-1.2.6-win32-bin.zip下载地址: http://code.jellycan.com/memcached/ ...

  6. Memcached常用命令及使用说明

    一.存储命令 存储命令的格式: 1 2 <command name> <key> <flags> <exptime> <bytes> < ...

  7. Memcached总结三:Memcached常用命令及使用说明

    一.存储命令 存储命令的格式: 1 2 <command name> <key> <flags> <exptime> <bytes> < ...

  8. springMVC整合memcached,以注解形式使用

    睡不着,深夜写点博客.闲下来有一个月了,心里多少有点…… 在北京找工作一再受阻,这个时间点也不好找 再接再厉 之前没有用过memcached,没有什么实战经验,看了一些关于memcached的博客,写 ...

  9. 转:Memcached常用命令及使用说明

    一.存储命令 存储命令的格式: 1 2 <command name> <key> <flags> <exptime> <bytes> < ...

随机推荐

  1. 【sping揭秘】9、容器内部事件发布(二)

    写在前面---------------------------------- 命运多舛,痴迷淡然 不知下一步该往哪里走,现在应该是我的迷茫期... 加油,快点走出去!!! 聪明的网友们,你们有没有迷茫 ...

  2. 08-02 Java 代码块,代码块执行的先后顺序问题

    代码块 /* 代码块:在Java中,使用{}括起来的代码被称为代码块. 根据其位置和声明的不同,可以分为 局部代码块:局部位置,用于限定变量的生命周期. 构造代码块:在类中的成员位置,用{}括起来的代 ...

  3. MVC3学习:将excel文件导入到sql server数据库

    思路: 1.将excel文件导入到服务器中. 2.读取excel文件,转换成dataset. 3.循环将dataset数据插入到数据库中. 本例子使用的表格为一个友情链接表F_Link(LinkId, ...

  4. ORACLE数据库表解锁record is locked by another user

    出现此问题多由于操作Oracle执行完,没有COMMIT,直接把PL/SQL关闭掉,后来导致那张表被锁住,当编辑时就会出现这个信息,record is locked by another user! ...

  5. 全网最全的Windows下Anaconda2 / Anaconda3里正确下载安装用来定时任务apscheduler库(图文详解)

    不多说,直接上干货!  Anaconda2 里 PS C:\Anaconda2\Scripts> PS C:\Anaconda2\Scripts> pip.exe install apsc ...

  6. 12 二叉树-链式存储-二叉排序树(BST)

    呜呜 写这个东西花了我2天 居然花了两天!!我还要写AVL呢啊啊啊啊啊啊啊!!!!!! 等下还要跑去上自习 大早上起来脸都没洗现在先赶紧发博客 昨晚写出来了独自在其他人都睡着了的宿舍狂喜乱舞.. 迷之 ...

  7. Linux配置多个Tomcat同时运行

    Linux系统下怎样配置多个Tomcat同时运行呢,首先修改变量为第一个tomcat,然后修改第二个tomcat启动的脚本 1.修改环境变量 # vi /etc/profile ####### 工程1 ...

  8. 数据库字典查询,从EF工具操作中提取的

    用户表: SELECT [Project1].[C1] AS [C1] , [Project1].[CatalogName] AS [CatalogName] , [Project1].[Schema ...

  9. Java源码阅读(不断补充)

    java.util.LinkedList LinkedList是实现了List接口的双链表实现,拥有list的所有方法并且允许所有元素(包括null). 双向链表也叫双链表,是链表的一种,它的每个数据 ...

  10. ApiGen安装

    # 首先,下载ApiGen(http://apigen.org/apigen.phar) # *nix系统 下载phar文件后, 移动到PATH目录中,以保证全局有权限调用 $ mv apigen.p ...