[转载]Flex4 Net服务端内存数据管理 – 4K.Yu 刻录时光 – 博客园.
使用Flex+net开发程序,但还是停留在原来的web习惯上,想用session之类存储一个后台数据,但是,使用的不是webservice 方式,而且remoting方式,所以服务端没有session,那该怎么存储服务端的数据呢?当然是暂存,因为还没必要立刻插入到数据库中。
考 虑了好久,也google&baidu了好久,始终不得解。后来觉得是不是我的想法有问题,或许现在改用flex做前台,就不需要在服务器端暂存 数据了,我们已经把瘦客户端变成富客户端,同理,现在是瘦服务端,所以将中间数据暂存在客户端,然后服务端用的时候从客户端取呢?
后来考虑了,还是将数据存储在服务端,具体思路:
写一个单例Comm类来管理所有连接的数据,再用一个Vars类来具体管理每一个连接的数据。
Comm类部分代码:
代码
private volatile static Comm instance = null;
private static object lockHelper = new object(); private Comm()
{
SQLConn = System.Configuration.ConfigurationManager.ConnectionStrings[“brxConn“].ConnectionString;
WebSite = System.Configuration.ConfigurationManager.AppSettings[“WebSite“].ToString();
}
public static Comm Instance
{
get
{
if (instance == null)
{
lock (lockHelper)
{
if (instance == null)
{
instance = new Comm();
}
}
}
return instance;
}
}
public static void ResetComm()
{
instance = null;
}
private static object lockHelper = new object(); private Comm()
{
SQLConn = System.Configuration.ConfigurationManager.ConnectionStrings[“brxConn“].ConnectionString;
WebSite = System.Configuration.ConfigurationManager.AppSettings[“WebSite“].ToString();
}
public static Comm Instance
{
get
{
if (instance == null)
{
lock (lockHelper)
{
if (instance == null)
{
instance = new Comm();
}
}
}
return instance;
}
}
public static void ResetComm()
{
instance = null;
}
然后在Comm中定义一个Hashtable ht来存储所有的Vars实例,各个连接用Guid来获取其对应的Vars:
代码
private Hashtable ht;//hashtable to store vars
public Vars getVarsbyGuid(string guid)
{
if (string.IsNullOrEmpty(guid))
return null;
if (ht == null)
ht = new Hashtable();
//删除过期的vars,delete vars before 30 mins
Hashtable newHt = new Hashtable();
foreach (DictionaryEntry de in ht)
{
if (((Vars)de.Value).LastUpdate > DateTime.Now.AddMinutes(–30))
{
newHt.Add(de.Key, de.Value);
}
}
ht = newHt;
Vars myv = null;
if (!ht.ContainsKey(guid))
{
myv = new Vars();
myv.LastUpdate = DateTime.Now;
ht.Add(guid, myv);
}
else
{
myv = (Vars)ht[guid];
myv.LastUpdate = DateTime.Now;
}
return (Vars)ht[guid];
}
public Vars getVarsbyGuid(string guid)
{
if (string.IsNullOrEmpty(guid))
return null;
if (ht == null)
ht = new Hashtable();
//删除过期的vars,delete vars before 30 mins
Hashtable newHt = new Hashtable();
foreach (DictionaryEntry de in ht)
{
if (((Vars)de.Value).LastUpdate > DateTime.Now.AddMinutes(–30))
{
newHt.Add(de.Key, de.Value);
}
}
ht = newHt;
Vars myv = null;
if (!ht.ContainsKey(guid))
{
myv = new Vars();
myv.LastUpdate = DateTime.Now;
ht.Add(guid, myv);
}
else
{
myv = (Vars)ht[guid];
myv.LastUpdate = DateTime.Now;
}
return (Vars)ht[guid];
}
同时为了节约内存,对超时的vars进行回收。
为了实现回收,需要在Vars中设置最后更新时间:
代码
public class Vars
{
/// <summary>
/// once lastupdate is 30minutes ago , we delete it from memory
/// </summary>
public DateTime LastUpdate { get; set; } public DataSet dataSet { get; set; }
public string notes { get; set; }
public string report { get; set; }
public string textHead { get; set; } public string regNo { get; set; }
public string companyName { get; set; }
public string finEnd { get; set; }
public string byType { get; set; }
public string byNet { get; set; }
public string bySingle { get; set; }
}
{
/// <summary>
/// once lastupdate is 30minutes ago , we delete it from memory
/// </summary>
public DateTime LastUpdate { get; set; } public DataSet dataSet { get; set; }
public string notes { get; set; }
public string report { get; set; }
public string textHead { get; set; } public string regNo { get; set; }
public string companyName { get; set; }
public string finEnd { get; set; }
public string byType { get; set; }
public string byNet { get; set; }
public string bySingle { get; set; }
}
最后,总结下思路:
1服务器启动初始化Comm
2客户端连接,获取Guid
3利用Guid从Comm创建或获取Vars
4每次获取Vars时同步更新时间
5每次获取Vars时扫描全部Hashtable,将超时的Vars回收
6客户端退出时从Comm销毁Vars