目录
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
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();
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
var builder = new ContainerBuilder();
builder.RegisterModule(new FlatwhiteCoreModule());
builder.RegisterType
.As
.EnableInterceptors();
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("*")
);
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
.Duration(2)
.VaryByCustom("custom")
.VaryByParam("postId")
.WithChangeMonitors((i, context) => { return new[] {new YouCustomCacheChangeMonitor()}; })
);