Flatwhite

目录

1 How to use Flatwhite?

1.1 For caching:

1.1.1  Enable class interceptor

1.1.2  Enable interface interceptor

1.1.3  Quick enable cache on all methods

1.1.4  Choose the method to cache without using Attribute filter

  1. How to use Flatwhite?
    1. For caching:
      1.  Enable class interceptor

If you modify the class to make the method virtual and decorate the method with OutputCacheAttribute, you will register the class like this:

public class UserService{

    [OutputCache(Duration = 2, VaryByParam = "userId")]

    public virtual object GetById(Guid userId)

{

// ...

}    }var builder = new ContainerBuilder();

builder.RegisterModule(new FlatwhiteCoreModule());

builder

.RegisterType()

.EnableInterceptors();

      1.  Enable interface interceptor

If the methods are not virtual, but the class implements an interface, you can decorate the methods on the interface with OutputCacheAttribute and register the type like this

public interface IUserService{

    [OutputCache(Duration = 2, VaryByParam = "userId")]

    object GetById(Guid userId);

    [NoCache]

    object GetByEmail(string email);

    IEnumerable GetRoles(Guid userId);}

var builder = new ContainerBuilder();

builder.RegisterModule(new FlatwhiteCoreModule());

builder.RegisterType()   

   .As()  

       .EnableInterceptors();

      1.  Quick enable cache on all methods

If you don't want to decorate the OutputCache attribute on the interface, you can do like this to enable cache on all methods

var builder = new ContainerBuilder();

builder.RegisterModule(new FlatwhiteCoreModule());

builder.RegisterType()   

   .As()  

       .CacheWithStrategy(CacheStrategies

.AllMethods()

.Duration(5)

.VaryByParam("*")

    );

      1.  Choose the method to cache without using Attribute filter

If you want to cache on just some methods, you can selectively do like below. Again, it works only on virtual methods if you are registering class service; interface services are fine.

var builder = new ContainerBuilder();

builder.RegisterModule(new FlatwhiteCoreModule());

builder.RegisterType()

   .As()

   .CacheWithStrategy(

           CacheStrategies.ForService()

      .ForMember(x => x.GetById(Argument.Any()))

  .Duration(2)

  .VaryByParam("postId")

                        

  .ForMember(x => x.GetComments(Argument.Any(), Argument.Any()))

      .Duration(2)

  .VaryByCustom("custom")

  .VaryByParam("postId")

  .WithChangeMonitors((i, context) =>   { return new[] {new YouCustomCacheChangeMonitor()};   })

       );

你可能感兴趣的:(Flatwhite,java,前端,服务器,开发语言)