memcached+狀態模式+工廠方法使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace memcached { public interface IStates { bool PreProduction(Context context,string FlowNo); //上機 bool PostProduction(Context context, string FlowNo);//下機 bool MaterialImput(Context context, string FlowNo);//物料錄入 } public class Context { public Context(IStates state) { this.currentState = state; } private string FlowNo; public string flowno { get { return FlowNo; } set { FlowNo = value; } } private IStates currentState; public IStates CurrentState { //set //{ // currentState = value; //} get { return currentState; } set { currentState = value; } } /// <summary> /// 执行動作 /// </summary> public virtual bool PreProduction() { return this.currentState.PreProduction(this, flowno); } public virtual bool PostProduction() { return this.currentState.PostProduction(this,flowno); } public virtual bool MaterialImput() { return this.currentState.MaterialImput(this,flowno); } } /// <summary> /// 空閒 /// </summary> public class OpenState : IStates { public bool PreProduction(Context context,string flowno) { Console.WriteLine("FLOW卡上機成功"); AMemcached.cache.Replace(flowno, "Opening"); return true; } public bool PostProduction(Context context, string flowno) { Console.WriteLine("FLOW卡還未物料錄入,無法下機"); return false; } public bool MaterialImput(Context context,string flowno) { Console.WriteLine("FLOW卡未上機不能進行物料錄入"); return false; } } /// <summary> /// 完成 /// </summary> public class CloseState : IStates { public bool PreProduction(Context context,string flowno) { Console.WriteLine("FLOW卡正在上機中,無法重複上機"); return false; } public bool PostProduction(Context context, string flowno) { Console.WriteLine("物料已錄入,成功下機"); //context.CurrentState = new OpenState(); AMemcached.cache.Replace(flowno, "OpenState"); var stateq = AMemcached.cache.Get(flowno); return true; } public bool MaterialImput(Context context, string flowno) { Console.WriteLine("物料已錄入,無法重複錄入"); return false; } } /// <summary> /// 生產中 /// </summary> public class Opening : IStates { public bool PreProduction(Context context,string flowno) { Console.WriteLine("FLOW卡正在上機中,無法重複上機"); return false; } public bool PostProduction(Context context, string flowno) { Console.WriteLine("物料還未錄入,無法下機"); return false; } public bool MaterialImput(Context context, string flowno) { Console.WriteLine("FLOW已上機,物料成功錄入"); // context.CurrentState = new CloseState(); AMemcached.cache.Replace(flowno, "CloseState"); var stateq = AMemcached.cache.Get(flowno); return true; } } }
using Memcached.ClientLibrary; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace memcached { class Program { static void Main(string[] args) { var state = AMemcached.cache.Get(DJ); Context check = new Context(Activator.CreateInstance(Type.GetType("memcached"+"."+ state)) as IStates); check.flowno = DJ; check.PreProduction();//上機 var states = AMemcached.cache.Get(DJ); check = new Context(Activator.CreateInstance(Type.GetType("memcached" + "." + states)) as IStates); check.flowno = DJ; check.PostProduction();//下機 var statee = AMemcached.cache.Get(DJ); check = new Context(Activator.CreateInstance(Type.GetType("memcached" + "." + statee)) as IStates); check.flowno = DJ; check.MaterialImput();//物料錄入 Console.ReadKey(); } } public class AMemcached { public static MemcachedClient cache; static AMemcached() { string[] servers = { "192.168.1.18:11211" }; //初始化池 SockIOPool pool = SockIOPool.GetInstance(); //设置服务器列表 pool.SetServers(servers); //各服务器之间负载均衡的设置比例 pool.SetWeights(new int[] { 1 }); //初始化时创建连接数 pool.InitConnections = 3; //最小连接数 pool.MinConnections = 3; //最大连接数 pool.MaxConnections = 5; //连接的最大空闲时间,下面设置为6个小时(单位ms),超过这个设置时间,连接会被释放掉 pool.MaxIdle = 1000 * 60 * 60 * 6; //socket连接的超时时间,下面设置表示不超时(单位ms),即一直保持链接状态 pool.SocketConnectTimeout = 0; //通讯的超市时间,下面设置为3秒(单位ms),.Net版本没有实现 pool.SocketTimeout = 1000 * 3; //维护线程的间隔激活时间,下面设置为30秒(单位s),设置为0时表示不启用维护线程 pool.MaintenanceSleep = 30; //设置SocktIO池的故障标志 pool.Failover = true; //是否对TCP/IP通讯使用nalgle算法,.net版本没有实现 pool.Nagle = false; //socket单次任务的最大时间(单位ms),超过这个时间socket会被强行中端掉,当前任务失败。 pool.MaxBusy = 1000 * 10; pool.Initialize(); cache = new MemcachedClient(); //是否启用压缩数据:如果启用了压缩,数据压缩长于门槛的数据将被储存在压缩的形式 cache.EnableCompression = false; //压缩设置,超过指定大小的都压缩 //cache.CompressionThreshold = 1024 * 1024; } } [Serializable] public class MpsFlowData { public string productno { get; set; } public string seqno { get; set; } public string keyno { get; set; } public string state { get; set;} } }