文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>大型webgame缓存研究 MemCached VB.Net C#

大型webgame缓存研究 MemCached VB.Net C#

时间:2010-11-13  来源:德鲁凯

 

简单说,刷新所有用户的缓存 页面读数据 换 cache  效率可想而知 

 

这里 我先贴出一个基类 VB.NET C#都有 呵呵 

 

C#版本

    string[] servers = { "127.0.0.1:11211", "127.0.0.1:11211" };
        SockIOPool pool;
        MemcachedClient mc;

        public Memcached()
        {
            //初始化池
            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();

            mc = new MemcachedClient();
            mc.EnableCompression = false;
        }

        public void Remove(string key)
        {
            mc.Delete(key);
        }

        public bool Set(string key, object value)
        {
            return mc.Set(key, value);
        }

        public bool Set(string key, object value, int minute)
        {
            return mc.Set(key, value, DateTime.Now.AddMinutes(minute));
        }

        public Hashtable Stats()
        {
            return mc.Stats();
        }

        public object Get(string key)
        {
            return mc.Get(key);
        }

        public bool ContainsKey(string key)
        {
            return mc.KeyExists(key);
        }

VB.NET

 

代码
 Dim servers() As String = {"127.0.0.1:11211", "127.0.0.1:11211"}

Dim pool As SockIOPool
Dim mc As New MemcachedClient

Public Function Memcached() As Integer
'初始化池
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()

mc
= New MemcachedClient()
mc.EnableCompression
= False
End Function

Public Sub RemoveMEM(ByVal key As String)
mc.Delete(key)
End Sub

Public Function SetMEM(ByVal key As String, ByVal value As Object) As Boolean
Return mc.Set(key, value)
End Function

Public Function SetMEM(ByVal key As String, ByVal value As Object, ByVal minute As Integer) As Boolean
Return mc.Set(key, value, DateTime.Now.AddMinutes(minute))
End Function

Public Function StatsMEM() As Hashtable
Return mc.Stats()
End Function

Public Function GetMEM(ByVal key As String) As Object
Return mc.Get(key)
End Function

Public Function ContainsKey(ByVal key As String) As Boolean
Return mc.KeyExists(key)
End Function

 

 

 

实际应用中 考虑到服务器重复使用的问题 

 

s1_u999_dt_uMain 

所有的key必须有 s1 类似开头,表明数据来自那个服务器  这样在一台服务器上多服公用就能很好的解决了   

u999 就是用户编号  

 

上例  来自 1服 编号999 用户的主表信息

 

s1_u999_str_bag

 

 

 

 

 

相关阅读 更多 +
排行榜 更多 +
梦幻甜心蛋糕店手游 v1.0 安卓版

梦幻甜心蛋糕店手游 v1.0 安卓版

休闲益智 下载
狙击手血战鬼子 v8081.23.10.7 安卓版

狙击手血战鬼子 v8081.23.10.7 安卓版

休闲益智 下载
狙击手血战鬼子 v8081.23.10.7 安卓版

狙击手血战鬼子 v8081.23.10.7 安卓版

休闲益智 下载