using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Memcached.ClientLibrary; namespace WebDemo.Controllers { public class MemcachedController : BaseController { ActionResult Index() { //初始化memcached 服务器端集群列表。 String[] serverlist = { }; // initialize the pool for memcache servers SockIOPool pool = SockIOPool.GetInstance(); //设置怎么mem池连接点服务器端。 pool.SetServers(serverlist); pool.Initialize(); mc = new MemcachedClient(); mc.PoolName = ; mc.EnableCompression = false; //mc.Add("gz1", "我的女神宋智孝"); mc.Set(, , DateTime.Now.AddSeconds(15)); pool.Shutdown();Content(); } } }
View Code
2、models:
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; namespace WebDemo.Models { public class SchoolDbContext :DbContext { SchoolDbContext() : ) { this.Database.CreateIfNotExists(); } public virtual DbSet<Student> Student { get; set; } public virtual DbSet<UserInfo> UserInfo { get; set; } } }
View Code
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace WebDemo.Models { [Serializable] public class Student { [StringLength(32)] SName { get; set; } [StringLength(32)] Address { get; set; } [Key] Id { get; set; } } }
View Code
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Web; namespace WebDemo.Models { [Serializable] public class UserInfo { public string UName { get; set; } [Required] [MaxLength(32)] public string UPwd { get; set; } [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } } }
View Code3、接下来的代码是使用分布式缓存中最关键的一点: