How to use Flatwhite?

目录

1 How to use Flatwhite?

1.1 For caching:

1.1.1 / Enable interceptors on all previous registrations

1.1.2 / Auto refresh stale data

1.1.3 / Using CacheProfile

1.1.4 / Revalidate cache

  1. How to use Flatwhite?
    1. For caching:
      1. / Enable interceptors on all previous registrations

If you're a fan of assembly scanning, you can decorate the OutputCache attribute on classes & interfaces you want to cache and enable them by RegisterModule FlatwhiteBuilderInterceptModule before building the container

var builder = new ContainerBuilder();

builder

.RegisterType<BlogService>()

.AsImplementedInterfaces()

.AsSelf();

// Register other types normally

...

// Register FlatwhiteBuilderInterceptModule at the end

builder.RegisterModule<FlatwhiteBuilderInterceptModule>();            var container = builder.Build();

      1. / Auto refresh stale data

Flatwhite can auto refresh the stale content if you set StaleWhileRevalidate with a value greater than 0. This should be used with Duration to indicates that caches MAY serve the cached result in which it appears after it becomes stale, up to the indicated number of seconds The first call comes to the service and gets a stale cache result will also make the cache system auto refresh once in the background. So if the method is not called many times in a short period, it's better to turn on AutoRefresh to make the cache alive and refreshed as soon as it starts to be stale

public interface IBlogService{

// For method with too many cache variations because of VaryByParam settings

    [OutputCache(Duration = 5, VaryByParam = "tag, from, authorId", StaleWhileRevalidate = 5)]

    IEnumerable<object> Search(string tag, DateTime from, Guid authorId);    

// For method with not many cache variations and data is likely to changed every 5 seconds

    [OutputCache(Duration = 5, VaryByParam = "blogId", StaleWhileRevalidate = 5)]

    object GetById(int blogId);    

// You can turn on AutoRefresh to keep the cache active if there are limited variations of the cache

    [OutputCache(Duration = 5, VaryByParam = "blogId", StaleWhileRevalidate = 5, AutoRefresh = true)]

    IEnumerable<string> GetBlogCategory();    }

      1. / Using CacheProfile

public interface IUserService{

    [OutputCache(CacheProfile="profileName")]

    object GetById(Guid userId);}

Profile setting default file name is cacheProfile.yaml located at the same folder of your app.config/web.config file and has a "yaml like" format:

-- Cache profile settings, everything is case-sensitive-- Profile nameProfile1-Two-Seconds -- Profile propertyName:propertyValue, start with a tab or 4+ empty spaces Duration:2 StaleWhileRevalidate:5 VaryByParam:packageId VaryByCustom:* AutoRefresh:true RevalidateKeyFormat:anything-about-user{userId} Web-Profile2-Three-Seconds MaxAge:3 StaleWhileRevalidate:6 VaryByParam:* VaryByHeader:UserAgent IgnoreRevalidationRequest:true

You can implement another IOutputCacheProfileProvider and set to Global.OutputCacheProfileProvider or simply change the location/name of the yaml file. At the moment, only yaml file is supported.

      1. / Revalidate cache

Even though you can use AutoRefresh or StaleWhileRevalidate to auto refresh cache data. Some time you want to remove the cache item after you call a certain method. You can use RevalidateAttribute to remove the cache item or some related cache items. Decorate the attribute on another method and the cache item will be removed once the method is invoked successfully. On example below, when you call method DisableUser, because it has the Revalidate attribute decorated with "User_{userId}" as the key format, all related cache items created for method with attribute OutputCache which has RevalidateKeyFormat = "User_{userId}" will be reset.

public interface IUserService{

    [OutputCache(Duration = 2, StaleWhileRevalidate = 2, VaryByParam = "userId", RevalidateKeyFormat = "User_{userId}")]

object GetById(Guid userId);

[OutputCache(Duration = 2, VaryByParam = "userId", RevalidateKeyFormat = "User_{userId}")]

Task<object> GetByIdAsync(Guid userId);

[Revalidate("User_{userId}")]

void DisableUser(Guid userId);  }

Unfortunately, this is not working for distributed services. That means the method is called on one server cannot notify the other service instances on remote servers. However, it's technically achievable to extend this filter using distributed messaging or something like that to notify remote system.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_74456535

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值