我正在尝试使用CacheCow实现缓存。我有两个问题:
purchase的资源,还有一个称为pointMovements的资源。它们并不是完全相连的,但是在purchase中写一篇文章,意味着pointMovement中的一些变化。Cachecow没有检测到这些更改,因为我没有调用pointmovements的API。因此,当我调用pointmovements的端点时,会缓存这些值,并且无法获得新的值。
为了解决这个问题,我需要手动使其失效,这怎么可能呢?发布于 2014-08-22 01:18:31
我遇到了同一组问题,并找到了问题2的解决方案(不管默认设置如何,都禁用缓存)。
// This forces the server to not provide any caching by refreshing its cache table immediately (0 sec)
[HttpCacheRefreshPolicy(0)]
// This forces the client (browser) to not cache any data returned from the server (even if ETag is present) by setting the time-out to 0 and no-cache to true.
[HttpCacheControlPolicy(true, 0, true)]
public void MyController : ApiControler {... }这些属性必须一起应用,这样才能工作。还可以通过向每个操作提供相同的规则,在操作级别控制缓存。
我仍然需要找到问题1的解决方案。但是,请注意这个空间以进行更新。
更新我找到了问题1的解决方案。
CachingHandler注册到您的IoC容器(在我的例子中是IUnityContainer)ICachingHandler注入到Web控制器中。ICachingHandler.InvalidateResource(HttpRequestMessage)请参阅下面的代码示例。解决方案已经过测试。
public class Bootstrapper
{
//...
// Create a new caching handler and register it with the container.
public void RegisterCache(HttpConfiguration config, IUnityContainer container)
{
var cachingHandler = new CachingHandler(config);
// ...
container.RegisterInstance<ICachingHandler>(cachingHandler);
}
}
public class ResourceContoller : ApiController
{
private ICachingHandler _cachingHandler;
public ResourceContoller(ICachingHandler cachingHandler)
{
_cachingHandler = cachingHandler;
}
[HttpPost]
public void DeleteResource(int resourceId)
{
// Do the delete
// ...
// Now invalidate the related resource cache entry
// Construct a http request message to the related resource
// HINT: The "DefaultApi" may not be your api route name, so change this to match your route.
// GOTCHA: The route matching mechanism is case sensitive, so be aware!
var relatedResource = new HttpRequestMessage(HttpMethod.Get, Url.Link("DefaultApi", new {controller = "linkedresource", action = "getlinkedresource", id: resourceId}));
// Invalidate the resource with the caching handler.
_cachingHandler.InvalidateResource(relatedResource);
}
}发布于 2014-10-08 10:19:45
抱歉反应太晚了。
正如@Tri q所说,这样做的方法是使用我在这个博客中解释过的属性:
http://byterot.blogspot.co.uk/2013/03/rest-asp-net-wep-api-0.4-new-features-breaking-change-cachecow-server.html
https://stackoverflow.com/questions/24819537
复制相似问题