SP 2013 - SPCache类。这个怎么运作? [英] SP 2013 - SPCache Class. How it works?
问题描述
我需要有关spcache类内部工作原理的信息,而不是如何实现它。
https://msdn.microsoft.com/en-us/library/microsoft.office.server.objectcache.spcache.aspx?f=255&MSPPError=-2147217396
<我无法找到解释服务器如何管理和存储使用spcache创建的对象的技术文章,所以我对这个主题有疑问:
- 它是在前端服务器之间共享的吗?
- 它在农场范围内?
- 它是附加到特定数据库还是AppFabric?
任何建议都将不胜感激。
Andrea。
嗨Andrea,
这不是Farm Wide。本质上,它是使用它的服务器内存中的线程安全字典。检查下面的主题。
https: //sharepoint.stackexchange.com/questions/10778/is-spcache-a-farm-wide-cache
如何使用SPCache的示例。
//用于缓存的对象
公共类TestClass
{
public string someData {get;组; }
public TestClass(string myData)
{
this.someData = myData;
}
}
// SPCache对象设置缓存参数
公共类MyCache
{
public static SPCacheConfig cacheConfig = new SPCacheConfig();
内部静态CacheParameter [] CacheParameters = new CacheParameter []
{
// DataCache
新CacheParameter(ServiceName,
DataCache,
0xfffff,
PriorityType.High,
new TimeSpan(0,60,0))
};
internal const string ServiceName =" CacheAppStressTest" ;;
内部const字符串DataCache =" CacheAppStressTest.Data" ;;
}创建Web部件并添加以下代码。
public class CustomCache:WebPart
{
Label lblMessage;
Label lblCacheData;
public CustomCache()
{
this.lblMessage = new Label();
this.lblMessage.ID =" lblMessage" ;;
this.lblCacheData = new Label();
this.lblCacheData.ID =" lblCacheData" ;;
}
protected override void CreateChildControls()
{
try
{
byte priority = 0;
MyCache.CacheParameters [0] .lifetime = new TimeSpan(0,1,0); //设置参数
的生命周期MyCache.CacheParameters [0] .priority =(PriorityType)priority; //设置参数优先级
MyCache.cacheConfig.AddCaches(MyCache.CacheParameters);
SPCache.Cache.Put(MyCache.DataCache,new SPCachedObject(" Test",new TestClass(" some data"),new TimeSpan(1,0,0))); //将缓存对象添加到SPCache Collection
TestClass newClass =(TestClass)SPCache.Cache.Get(MyCache.DataCache," Test");
if(newClass!= null)
{
this.lblCacheData.Text ="< br />< b>" + newClass.someData +"< / b>" ;;
}
其他
{
this.lblCacheData.Text ="< br />< b>空数据< / b>" ;;
}
//删除缓存对象
SPCache.Cache.Delete(MyCache.DataCache," Test");
//显示缓存数据
this.lblMessage.Text = string.Format(" CacheHits = {0} \r\\\
Reads = {1} \\\
Count = {2} \\ nSizeBytes = {3} \\\
HitRatio = {4}",
SPCache.Cache.TotalCacheHits,SPCache.Cache.TotalReadAttempts,SPCache.Cache.Count,SPCache.Cache.UsedBytes ,SPCache.Cache.HitRatio);
MyCache.cacheConfig.RemoveCaches(MyCache.CacheParameters);
}
catch(Exception ex)
{
this.lblMessage.Text = ex.Message;
}
this.Controls.Add(this.lblMessage);
this.Controls.Add(this.lblCacheData);
}
}
最好的问候,
Dennis
Hi,
I need information on how the spcache class internally works, not how to implement it.
https://msdn.microsoft.com/en-us/library/microsoft.office.server.objectcache.spcache.aspx?f=255&MSPPError=-2147217396
I'm unable to find a technical article that explain how the server manage and store objects created with spcache, so I have doubt regarding this topics:
- It's shared between Front-End Servers?
- It's farm wide?
- It's attached to specific database or to AppFabric?
Any suggestions would be greatly appreciated.
Andrea.
Hi Andrea,
It's not Farm Wide. Essentially it is a thread-safe Dictionary in memory of the server that uses it. Check the thread below.
https://sharepoint.stackexchange.com/questions/10778/is-spcache-a-farm-wide-cache
Example for how to use SPCache.
//Object for Cacheing public class TestClass { public string someData { get; set; } public TestClass(string myData) { this.someData = myData; } }
// SPCache Object Settings Cache Parameters public class MyCache { public static SPCacheConfig cacheConfig = new SPCacheConfig(); internal static CacheParameter[] CacheParameters = new CacheParameter[] { // DataCache new CacheParameter(ServiceName, DataCache, 0xfffff, PriorityType.High, new TimeSpan(0,60,0)) }; internal const string ServiceName = "CacheAppStressTest"; internal const string DataCache = "CacheAppStressTest.Data"; }Create Web part and add following code.
public class CustomCache : WebPart { Label lblMessage; Label lblCacheData; public CustomCache() { this.lblMessage = new Label(); this.lblMessage.ID = "lblMessage"; this.lblCacheData = new Label(); this.lblCacheData.ID = "lblCacheData"; } protected override void CreateChildControls() { try { byte priority = 0; MyCache.CacheParameters[0].lifetime = new TimeSpan(0, 1, 0); //Set life time of parameter MyCache.CacheParameters[0].priority = (PriorityType)priority; // Set priority of paramater MyCache.cacheConfig.AddCaches(MyCache.CacheParameters); SPCache.Cache.Put(MyCache.DataCache, new SPCachedObject("Test", new TestClass("some data"), new TimeSpan(1, 0, 0)));// Add cache object into SPCache Collection TestClass newClass = (TestClass)SPCache.Cache.Get(MyCache.DataCache, "Test"); if (newClass != null) { this.lblCacheData.Text = "<br /><b>" + newClass.someData + "</b>"; } else { this.lblCacheData.Text = "<br /><b> Null data</b>"; } // Remove cache object SPCache.Cache.Delete(MyCache.DataCache, "Test"); //Display Cache data this.lblMessage.Text = string.Format("CacheHits={0}\r\nReads={1}\r\nCount={2}\r\nSizeBytes={3}\r\nHitRatio={4}", SPCache.Cache.TotalCacheHits, SPCache.Cache.TotalReadAttempts, SPCache.Cache.Count, SPCache.Cache.UsedBytes, SPCache.Cache.HitRatio); MyCache.cacheConfig.RemoveCaches(MyCache.CacheParameters); } catch (Exception ex) { this.lblMessage.Text = ex.Message; } this.Controls.Add(this.lblMessage); this.Controls.Add(this.lblCacheData); } }Best Regards,
Dennis
这篇关于SP 2013 - SPCache类。这个怎么运作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!