在WCF中缓存?

Dev*_*per 42 .net c# wcf

我正在构建一个WCF服务.我需要将参考数据存储在缓存中,每当我从方法接收输入时我都会查找...这样做的正确方法是什么?我还想为缓存定义一个到期策略,该策略将在一定时间间隔后使其失效.

Juo*_*nis 30

如果您使用的是.NET 4,建议使用MemoryCache

  • 看看这篇博客文章,了解如何使用它:http://pieterderycke.wordpress.com/2012/04/09/caching-in-wcf-services-part-1/ (7认同)
  • 只需确保丢弃内存缓存,否则会有很好的内存泄漏.我知道......这花费了我生命中的几天. (2认同)

Sri*_*ake 28

任何缓存解决方案都应解决两个基本问题

1)存储缓存项和检索

2)缓存失效

由于Http缓存是一个众所周知的缓存,我不打算详细解释它.您可以单独使用asp兼容性属性和一些Web配置,您可以通过魅力获得缓存.

[AspNetCacheProfile("MyProfile")]
        public Customer GetName(string id)
        {
             // ...
        }
Run Code Online (Sandbox Code Playgroud)

而web配置就像

<system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />    
</system.serviceModel>
<system.web>
   <caching>
      <outputCacheSettings>
         <outputCacheProfiles>
            <add name=" MyProfile" duration="600" varyByParam="none" sqlDependency="MyTestDatabase:MyTable"/>
         </outputCacheProfiles>
      </outputCacheSettings>
   </caching>
</system.web>
Run Code Online (Sandbox Code Playgroud)

但这并不适合大多数场景,尤其是当您有大型复杂对象进行缓存时.例如,我有一种情况,我想缓存系统生成的图像(操作合同的输出是系统生成的图像,取决于输入).在这种情况下,您必须实现自己的缓存.我使用了Microsoft企业库缓存块来解决我的所有缓存存储需求.但是,您仍需要执行管道以将Microsoft企业库缓存块与WCF服务集成.首先,您必须拦截WCF通信通道以实现缓存.有关如何拦截WCF通信通道的详细讨论,请访问http://msdn.microsoft.com/en-us/magazine/cc163302.aspx.这是您如何为WCF缓存执行管道工作

基本的管道架构

步骤0 假设您有一个操作合同,如下所示,您希望通过该方法缓存项目返回.

[OperationContract]
MyCompositeClass Rotate(int angle)
Run Code Online (Sandbox Code Playgroud)

步骤1 首先,您必须在WCF管道中注册自定义cacher.为此,我将使用一个属性,以便我可以根据面向方面的编程原则很好地装饰我的WCF调用.

using System;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Reflection;

    [AttributeUsage(AttributeTargets.Method)]
    public class MyCacheRegister : Attribute, IOperationBehavior
    {
        ConstructorInfo _chacherImplementation;
        public ImageCache(Type provider)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("Provider can't be null");
            }
            else if (provider.IsAssignableFrom(typeof(IOperationInvoker)))
            {
                throw new ArgumentException("The type " + provider.AssemblyQualifiedName + " does not implements the interface " + typeof(IOperationInvoker).AssemblyQualifiedName);
            }
            else
            {
                try
                {
                    Type[] constructorSignatureTypes = new Type[1];
                    constructorSignatureTypes[0] = typeof(IOperationInvoker);
                    _chacherImplementation = provider.GetConstructor(constructorSignatureTypes);

                }
                catch
                {
                    throw new ArgumentException("There is no constructor in " + provider.AssemblyQualifiedName + " that accept " + typeof(IOperationInvoker).AssemblyQualifiedName + " as a parameter");
                }

            }


        }

        public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
        {
            return;
        }

        public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
        {
            return;
        }

        /// <summary>
        /// Decorate the method call with the cacher
        /// </summary>
        public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
        {
            //decorator pattern, decorate with a  cacher
            object[] constructorParam = new object[1];
            constructorParam[0] = dispatchOperation.Invoker;
            dispatchOperation.Invoker = (IOperationInvoker)_chacherImplementation.Invoke(constructorParam);
        }

        public void Validate(OperationDescription operationDescription)
        {
            return;
        }
    }
Run Code Online (Sandbox Code Playgroud)

第2步

然后,您必须实现将检索缓存对象的点.

using System;
using System.ServiceModel.Dispatcher;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.IO;

    class RotateCacher : IOperationInvoker
    {

        private IOperationInvoker _innerOperationInvoker;
        public RotateImageCacher(IOperationInvoker innerInvoker)
        {
            _innerOperationInvoker = innerInvoker;
        }
        public object[] AllocateInputs()
        {
            Object[] result = _innerOperationInvoker.AllocateInputs();
            return result;
        }

        public object Invoke(object instance, object[] inputs, out object[] outputs)
        {
            object result=null;

///TODO: You will have more object in the input if you have more ///parameters in your method

            string angle = inputs[1].ToString();

            ///TODO: create a unique key from the inputs
            string key = angle;

            string provider = System.Configuration.ConfigurationManager.AppSettings["CacheProviderName"];
            ///Important Provider will be DiskCache or MemoryCache for the moment
provider =”DiskCache”;
///TODO: call enterprise library cache manager, You can have your own 
/// custom cache like Hashtable

    ICacheManager manager = CacheFactory.GetCacheManager(provider);

            if (manager.Contains(key))
            {

                result =(MyCompositeClass) manager[key];

            }
            else
            {
                result =(MyCompositeClass) _innerOperationInvoker.Invoke(instance, inputs, out outputs);
                manager.Add(key, result);
            }
            return result;
        }

        public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
        {
            IAsyncResult result = _innerOperationInvoker.InvokeBegin(instance, inputs, callback, state);
            return result;
        }

        public object InvokeEnd(object instance, out object[] outputs, IAsyncResult asyncResult)
        {
            object result = _innerOperationInvoker.InvokeEnd(instance, out outputs, asyncResult);
            return result;
        }

        public bool IsSynchronous
        {
            get { return _innerOperationInvoker.IsSynchronous; }
        }
    }
Run Code Online (Sandbox Code Playgroud)

第3步

最后在服务电话上方添加您的属性

[OperationContract]
[MyCacheRegister(typeof(RotateCacher)]
MyCompositeClass Rotate(int angle)
Run Code Online (Sandbox Code Playgroud)

企业库缓存块的配置超出了本答案的范围.您可以使用以下链接来学习它.企业库的好处在于您已经准备好了扩展缓存策略的方法.它内置了缓存过期和存储的方法.您还可以编写自己的缓存过期和存储策略. http://www.codeproject.com/KB/web-cache/CachingApplicationBlock.aspx

最后,为了让您的企业库缓存正常工作,您需要添加以下配置详细信息.您还需要在项目参考中添加相关的dll.

<configSections>
    <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>


  <cachingConfiguration defaultCacheManager="Cache Manager">
    <cacheManagers>
      <add name="MemoryCache" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"
        numberToRemoveWhenScavenging="10" backingStoreName="NullBackingStore" />
      <add name="DiskCache" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"
        numberToRemoveWhenScavenging="10" backingStoreName="IsolatedStorageCacheStore" />
    </cacheManagers>
    <backingStores>
      <add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="NullBackingStore" />
      <add name="IsolatedStorageCacheStore" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.IsolatedStorageBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        encryptionProviderName="" partitionName="MyCachePartition" />
    </backingStores>
  </cachingConfiguration>
Run Code Online (Sandbox Code Playgroud)


Ron*_*erg 1

你可以看看Velocity。这是微软的分布式内存缓存框架。但这可能有点太测试版了......