首页 技术 正文
技术 2022年11月15日
0 收藏 960 点赞 3,530 浏览 6291 个字
通过spring.net中的spring.caching CacheResult实现memcached缓存
1.SpringMemcachedCache.cs
2.APP.config
3.Program.cs
4.Common
待解决问题:CacheResult,CacheResultItems有什么区别????
SpringMemcachedCache.cs   memcached的实现, 继承了Spring.Caching.AbstractCache, memcached的实现用了Enyim.Caching
 using System;
using Spring.Caching;
using System.Web;
using System.Collections;
using System.Web.Caching;
using Enyim.Caching;
using Enyim.Caching.Memcached; namespace Demo.Common
{
/// <summary>
/// An <see cref="ICache"/> implementation backed by ASP.NET Cache (see <see cref="HttpRuntime.Cache"/>).
/// </summary>
/// <remarks>
/// <para>
/// Because ASP.NET Cache uses strings as cache keys, you need to ensure
/// that the key object type has properly implemented <b>ToString</b> method.
/// </para>
/// <para>
/// Despite the shared underlying <see cref="HttpRuntime.Cache"/>, it is possible to use more than
/// one instance of <see cref="AspNetCache"/> without interfering each other.
/// </para>
/// </remarks>
/// <author>Aleksandar Seovic</author>
/// <author>Erich Eichinger</author>
public class SpringMemcachedCache : AbstractCache
{
#region Fields // logger instance for this class
private static MemcachedClient memcachedClient = null;
private static object initLock = new object(); #endregion private MemcachedClient MemcachedClient
{
get
{
if (memcachedClient == null)
{
lock (initLock)
{
if (memcachedClient == null)
{
memcachedClient = new MemcachedClient();
}
}
} return memcachedClient;
}
} /// <summary>
/// Retrieves an item from the cache.
/// </summary>
/// <param name="key">
/// Item key.
/// </param>
/// <returns>
/// Item for the specified <paramref name="key"/>, or <c>null</c>.
/// </returns>
public override object Get(object key)
{
object result = key == null ? null : MemcachedClient.Get(GenerateKey(key));
return result;
} /// <summary>
/// Removes an item from the cache.
/// </summary>
/// <param name="key">
/// Item key.
/// </param>
public override void Remove(object key)
{
if (key != null)
{
MemcachedClient.Remove(GenerateKey(key));
}
} /// <summary>
/// Inserts an item into the cache.
/// </summary>
/// <param name="key">
/// Item key.
/// </param>
/// <param name="value">
/// Item value.
/// </param>
/// <param name="timeToLive">
/// Item's time-to-live (TTL).
/// </param>
protected override void DoInsert(object key, object value, TimeSpan timeToLive)
{
MemcachedClient.Store(StoreMode.Set, GenerateKey(key), value, timeToLive);
} /// <summary>
/// Generate a key to be used for the underlying <see cref="Cache"/> implementation.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string GenerateKey(object key)
{
return key.ToString();
} public override ICollection Keys
{
get { throw new NotSupportedException(); }
}
}
}

APP.config  配置enyim.com, memcache, spring.net

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="enyim.com">
<section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching"/>
<!-- log -->
<section name="log" type="Enyim.Caching.Configuration.LoggerSection, Enyim.Caching" />
</sectionGroup> <sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections> <enyim.com>
<memcached>
<servers>
<add address="127.0.0.1" port=""/>
</servers>
<socketPool minPoolSize="" maxPoolSize="" connectionTimeout="00:00:10" deadTimeout="00:02:00"/>
</memcached>
</enyim.com> <spring>
<context>
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net" >
<object id="CacheAspect" type="Spring.Aspects.Cache.CacheAspect, Spring.Aop"/>
<object id="autoBOProxyCreator" type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxyCreator, Spring.Aop">
<property name="ObjectNames">
<list>
<value>*BO</value>
</list>
</property>
<property name="InterceptorNames">
<list>
<value>CacheAspect</value>
</list>
</property>
</object> <object id="cachedTestBO" type="Demo.App.CachedTestBO, Demo.App"></object> <object id="MemCachedCache" type="Demo.Common.SpringMemcachedCache, Demo.Common">
<property name="TimeToLive" value="00:05:00"/>
</object> <object id="MemCachedCache-Short" type="Demo.Common.SpringMemcachedCache, Demo.Common">
<property name="TimeToLive" value="00:05:00"/>
</object> <object id="MemCachedCache-Long" type="Demo.Common.SpringMemcachedCache, Demo.Common">
<property name="TimeToLive" value="00:30:00"/>
</object> </objects>
</spring> </configuration>

App Program.cs       测试效果

1.要从spring中进入的才会调用, 直接实例化进入不会调用

2.CacheName – “SpringCache” 对应spring配置中的id=”SpringCache”节点,是实现cache的类

3.Key=”‘prefix.key.’+#cacheId” 传人cachedId=1对应生成以后的key为 prefix.key.1
复杂点的Key组合见下面

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Demo.Common;
using Spring.Caching;
using Spring.Context;
using Spring.Context.Support;namespace Demo.App
{
class Program
{
private static void Main(string[] args)
{
//通过spring.net来调用函数
IApplicationContext ctx = ContextRegistry.GetContext(); IDictionary testDictionary = ctx.GetObjectsOfType(typeof(ITestBO));
foreach (DictionaryEntry entry in testDictionary)
{
string name = (string)entry.Key;
ITestBO service = (ITestBO)entry.Value;
Console.WriteLine(name + " intercept: ");
          
//第一次运行函数并保存到memcached
Console.WriteLine("nowTime:" + DateTime.Now.ToString("T")
+ " testTime:" +service.TestMethod(, new object[]{"one", "two"})); Console.WriteLine(); Thread.Sleep(); //休眠5秒以便第二次调用查看是否有保存到memcached //第二次运行从memcached中读取数据
Console.WriteLine("nowTime:" + DateTime.Now.ToString("T")
+ " testTime:" + service.TestMethod(, new object[] { "one", "two" })); Console.WriteLine(); } //手动实例化,不会从memcached中读取数据,因为没有运行到CacheResult中去
ITestBO test = new CachedTestBO();
test.TestMethod(); Console.ReadLine();
}
} public interface ITestBO
{
string TestMethod(int cacheId, params object[] elements);
} public class CachedTestBO : ITestBO
{
//生成的key类似HK-MainSubAccountMap-1
[CacheResult(CacheName = SpringCache.NormalCache,
Key = SpringCache.SpringCacheCountryPrefix
+"+'" + SpringCacheKey.MainSubAccountMap + "'"
+ "+'-' + #cacheId")]
public string TestMethod(int cacheId, params object[] elements)
{
return DateTime.Now.ToString("T");
}
}}

Common类

 namespace Demo.Common
{
public static class ApplicationVariables
{
private static string countryCode;
public static string CountryCode
{
get
{
if (countryCode == null)
{
countryCode = "HK";
}
return countryCode;
}
}
} public static class SpringCache
{
public const string SpringCacheCountryPrefix = @"T(Demo.Common.ApplicationVariables).CountryCode + '-'";
public const string NormalCache = "MemCachedCache"; public const string AspnetCache = "AspnetCache";
} public static class SpringCacheKey
{
public const string MainSubAccountMap = "MainSubAccountMap"; public const string BOSetting = "BOSetting";
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,411
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,184
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,904