c#静态类 笔记

写成这样在调用,
MemcachedHelper.get(“abc”).ToString()
时会自动运行构造函数一次只运行一次

    public class MemcachedHelper {
        public static MemcachedClient mclient;

        static MemcachedHelper() {
            Logger.Info("memcachedClient constructor accessed");

            string[] serverlist = new string[] { "127.0.0.1:11211" };

            SockIOPool pool = SockIOPool.GetInstance("First");
            pool.SetServers(serverlist);
            pool.Initialize();
            mclient = new MemcachedClient();
            mclient.PoolName = "First";
            mclient.EnableCompression = false;
        }

        public static bool set(string key, object value) {
            return mclient.Set(key, value);
        }

        public static object get(string key) {
            return mclient.Get(key);
        }

    }

发表评论

电子邮件地址不会被公开。 必填项已用*标注