Talk in the AOP mode caching scheme

The method of cache set oriented data

As used autofac ioc container, using a method as Autofac.Extras.DynamicProxy2 blocker, a method for caching, add CachingAttribute method may be directly on the characteristic.

Cache registration process

  1. About interceptor method
  2. Name cache design
  3. Add ioc
  4. Add a binding interceptors and interface
  5. In a corresponding interface class, the adding method is CachingAttribute
  6. The role of cache get, remove the

About interceptor method

Interceptor method is an important embodiment of AOP aspect-oriented programming, our point of functional design appeared, decoupling and the existing system, and then injected into the project, what areas need this feature, you where to inject it.

AOP is the IOC container-based, so the interceptor component you need ahead of time to be injected into the ioc? Container, and, now? Either java or .net, have advocated a unified management objects, rather than to produce new objects it is also a reflection of the interface-oriented programming, objects are passed while you were in the business layer, is always dependent on the abstract rather than relying on the specific implementation!

Name cache design

Composed by the key name and value

  • key: Project name + name + object caching name (CachingAttribute.key)
  • value: + parameter name parameter value combination of a caching method
    , for example, the following method code cache
[Caching(CachingMethod.Get, value = "time")]
 DateTime GetTime(int id);

When the call is GetTime(1), the cache key value generated as follows:

key="DataSetCache_IUserInfoService_time"
value="id_1"
Add ioc
builder.RegisterType<CachingBehavior>();
            builder.RegisterType<DefaultUserInfoService>()
                   .As<IUserInfoService>()
                   .InstancePerLifetimeScope()
                   .InterceptedBy(typeof(CachingBehavior))
                   .EnableInterfaceInterceptors();

Add a binding interceptors and interface

 // 下面两句表示将CachingBehavior拦截器绑定到接口上
 .InterceptedBy(typeof(CachingBehavior))
 .EnableInterfaceInterceptors();

Add CachingAttribute properties in approach

[Caching(CachingMethod.Get,value="time")]
DateTime GetTime();

We can add this feature on the interface methods, you can add this feature in the method of interface implementation class, according to the degree of influence to add cache. If the cache on all interfaces start to realize the role, and laid them on the interface method, whereas the method on the class.

The role of cache get, remove the

  1. get: General method for acting on with a return value, the project name cache + + class name value, when a cache is present, the direct return, when there is, after performing the method, the return value into the cache and then return.
  2. remove: Delete a cache, it directly to the cache name value as a parameter to remove the implant.

Guess you like

Origin blog.51cto.com/14230003/2443710
Recommended