日期:2014-05-18  浏览次数:20905 次

memcache存放类的实例,多个程序读不到啊?
public partial class _Default : System.Web.UI.Page 
  { 
  public static MemcachedClient mc = null; 
  protected void Page_Load(object sender, EventArgs e) 
  { 
  string id = "1"; 
  String[] serverlist = { "127.0.0.1:11211" }; 
  int[] weights ={ 3 }; 
  //初始化池 
  SockIOPool pool = SockIOPool.GetInstance(); 
  pool.SetServers(serverlist); 
  pool.Nagle = false; 
  pool.Initialize(); 
  mc = new MemcachedClient(); 
  mc.EnableCompression = false; 

  gbtopic obj = new gbtopic(id, 1, id+"合理咯电456thu");//gbtopic 是自定义的类 
  mc.Set(id, obj); 

  Response.Write(mc.KeyExists(id).ToString());//这里显示true,键值“1”存在 
  } 
  } 
[Serializable] 
  public class gbtopic 
  { 
  public string id; 
  public string title; 
  public int num; 
  public gbtopic(string id, int num, string title) 
  { 
  this.id = id; 
  this.num = num; 
  this.title = title; 
  } 
  } 
==================================================== 
以上都没问题,一个asp.net网页创建类,存入Memcached,可以得到。但是我新建一个命令行程序 

class Program 
  { 
  public static MemcachedClient mc = null; 
  static void Main(string[] args) 
  { 
  string id = "1";  
  String[] serverlist = { "127.0.0.1:11211" }; 
  int[] weights ={ 3 }; 
  //初始化池 
  SockIOPool pool = SockIOPool.GetInstance(); 
  pool.SetServers(serverlist); 
  pool.Nagle = false; 

  pool.Initialize(); 

  mc = new MemcachedClient(); 

  mc.EnableCompression = false; 

  Console.WriteLine(mc.KeyExists(id).ToString())//这里显示false,为什么都是key为“1”这里读不到 
  Console.ReadLine(); 
  } 
  } 
我又做了试验,把那个asp.net网页 的mc.Set(id, obj); 
改成mc.Set(id, "strobj");//只存放一个string 
再运行命令行程序,就显示true了。 

问题总结: 
存放类的实例,本工程内可以正常读取,另一个程序读不到。但存在简单变量string,int等,多个程序都能读到。 
显示false的期间我用telnet 127.0.0.1 11211 
get 1 
试过,key为1的确实存在,但另一个程序mc.KeyExists(id)就是显示false,为什么?

------解决方案--------------------
这个是程序集跨域的问题,如果你实在要实现在上面保存类的实例的话,可以考虑使用Remoting之类的机制来实现
------解决方案--------------------
你把对象标记为可序列化试试
------解决方案--------------------
有深度啊!作个记号。