AppFabric 超时通知

发布于 2024-12-08 11:12:29 字数 593 浏览 0 评论 0原文

我已在 AppFabric 上启用通知,并且在添加期间指定的超时后从缓存中删除项目时,我尝试收到通知。 例如:

TimeSpan timeout = new TimeSpan(0,0,10); 
m_cache.Add(OrderId.Text, order, timeout);
m_cache.AddItemLevelCallback(OrderId.Text,DataCacheOperations.RemoveItem,myCacheLvlDelegate);

我在“myCacheLvlDelegate”方法中放置了一个断点,但即使在 10 秒超时(测试)之后也永远不会到达该断点。 出于测试目的,我在之后显式调用

 m_cache.Remove(OrderId.Text); 

,然后调用了委托!

因此,仅当我显式调用删除函数时才会调用委托方法,但如果超时到期则不会调用委托方法...

您是否有一种解决方案可以在超时后获取通知(在添加期间指定的通知)?

我需要它,因为我想在超时后调用 Web 服务来刷新数据并再次缓存结果。

谢谢你,

法布里斯

I have enabled notification on AppFabric and I'm trying to get notified when an item is removed from the cache after the timeout specified during the Add.
Ex :

TimeSpan timeout = new TimeSpan(0,0,10); 
m_cache.Add(OrderId.Text, order, timeout);
m_cache.AddItemLevelCallback(OrderId.Text,DataCacheOperations.RemoveItem,myCacheLvlDelegate);

I put a breakpoint in the "myCacheLvlDelegate" method but it is never reached even after the 10s timeout (test).
For test purpose, I have called explicitly

 m_cache.Remove(OrderId.Text); 

after and then the delegate was called !

So the delegate method is only called if I explicitly call the Remove function but not if the timeout expire...

Do you have a solution to get notify after the timeout (the one specified during the add) ?

I need it because I would like to call a webservice after the timeout to refresh data and cache the result again.

Thank you,

Fabrice

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

但可醉心 2024-12-15 11:12:29

您真正需要的是在特定时间间隔提醒您刷新 Appfabric 缓存项。

您可以尝试将 AppFabric 缓存与 Microsoft Enterprise Library 中的 Caching 块结合使用。缓存块使您能够在项目即将过期时收到通知。但这会创建两个缓存。

将对象标识符存储在应用程序块提供的缓存中,并具有必要的超时间隔,而您可以将实际数据“放入”AppFabric 缓存中,而无需超时间隔。使用“放置”而不是“添加”以确保替换对象(如果存在),否则创建它。

What you really need is to be reminded at specific intervals to refresh the Appfabric cache items.

You could try using the AppFabric cache in conjunction with the Caching block from the Microsoft Enterprise Library. The caching block provides you with the ability to be notified when the item is about to expire. This though will create two caches.

Store an object identifier in the cache provided by the application block with the necessary timeout interval, while you can "Put" the actual data in the AppFabric cache without a timeout interval. Use the "Put" instead of the "Add" to ensure that you replace the object if it exists else create it.

不可一世的女人 2024-12-15 11:12:29

我不认为你能够做到这一点,因为我不认为回调机制是为这种事情设计的。我可以看到您想要实现的目标,也可以看到您是如何得出这个问题的,但我认为只要您的客户端在您接触缓存的任何地方都使用缓存旁路模式,您就会得到影响。

为了进行审查,缓存旁模式为:

  • 检查缓存中是否有所需的项目
  • 如果该项目不在缓存中
    • 获取项目(从任何来源,例如数据库、网络服务)
    • 并放入缓存
    • 将项目返回给调用者
  • 如果项目在缓存中,则将
    • (将其转换为预期类型)
    • 将项目返回给调用者

例如

Order order;
// Check to see if the order is in the cache
Object cachedOrder = m_cache.Get(OrderId.Text);

if (cachedOrder == null)
{
    // The order is NOT in the cache, so get it from the web service
    order = OrderWebservice.Get(OrderId.Text);
    // Cache the order for 10 seconds
    m_cache.Add(OrderId.Text, order, New TimeSpan(0,0,10);
}
else
{
    // The order IS in the cache, so cast it
    order = (Order)cachedOrder;
}
// Return the order to the client, whether it's the cached order or the one from the web service
return order;

这样,缓存的订单永远不会超过十秒,因为如果超时超出缓存,下一个调用者将从缓存中获取空结果,并再次调用Web服务从Web服务获取订单。您将获得相同的效果,但由于缓存将按需填充,您应该会看到对服务器的影响较小,因为它不会每十秒旋转一次 Web 服务请求。

I don't think you'll be able to do this, as I don't think this sort of thing is what the callback mechanism was designed for. I can see what you're trying to achieve, and I can see how you've arrived at this question, but I think as long as your client uses the cache-aside pattern everywhere you're touching the cache you'll get the effect.

For review, the cache-aside pattern is:

  • Check the cache for the desired item
  • If the item is NOT in the cache
    • Get the item (from whatever source e.g. database, web service)
    • And put it into the cache
    • Return the item to the caller
  • If the item IS in the cache
    • (Cast it to the expected type)
    • Return the item to the caller

e.g.

Order order;
// Check to see if the order is in the cache
Object cachedOrder = m_cache.Get(OrderId.Text);

if (cachedOrder == null)
{
    // The order is NOT in the cache, so get it from the web service
    order = OrderWebservice.Get(OrderId.Text);
    // Cache the order for 10 seconds
    m_cache.Add(OrderId.Text, order, New TimeSpan(0,0,10);
}
else
{
    // The order IS in the cache, so cast it
    order = (Order)cachedOrder;
}
// Return the order to the client, whether it's the cached order or the one from the web service
return order;

In this way, the cached order will never be more than ten seconds old, because if it times out of the cache, the next caller will get a null result from the cache and call the web service again to get the order from the web service. You'll get the same effect, but as the cache will be filled on demand you should see a lower impact on your server as it won't be spinning up a web service request every ten seconds.

℡寂寞咖啡 2024-12-15 11:12:29

我现在不知道这个问题是否具有深刻的意义,但是如果 appfabric 中的某个项目过期,则在过期后将检索通知。即使是item级别的回调或者cache级别的回调。

我的代码是

CustomDataCacheOperations.InsertIntoCache(myTestCache, txtKey.Text, InputTable.Text, 10);    
 ndCacheLvlAllOps = myTestCache.AddItemLevelCallback(txtKey.Text, allCacheOperations, myCacheLvlDelegate);

插入方法是

public static void InsertIntoCache(DataCache curCache, string Key, object value,int timeoutInSeconds)
    {
        if (curCache.Get(Key) == null)
            curCache.Add(Key, value,new TimeSpan(0,0,timeoutInSeconds));
        else
            curCache.Put(Key, value, new TimeSpan(0, 0, timeoutInSeconds));
    }

并且它适当地发出所有操作的通知。过期通知仅在该项目从内存中被逐出后才会发出,而不是在 TTL(生存时间)结束时发出。我已经从微软确认了这一点。您应该稍等一下才能收到通知。

I don't now if the question is of deep meaning, but if an item expires in appfabric, the notification will be retrieved after it is expired. Even if it is item level callback or cache level call back.

My code is

CustomDataCacheOperations.InsertIntoCache(myTestCache, txtKey.Text, InputTable.Text, 10);    
 ndCacheLvlAllOps = myTestCache.AddItemLevelCallback(txtKey.Text, allCacheOperations, myCacheLvlDelegate);

And insert method is

public static void InsertIntoCache(DataCache curCache, string Key, object value,int timeoutInSeconds)
    {
        if (curCache.Get(Key) == null)
            curCache.Add(Key, value,new TimeSpan(0,0,timeoutInSeconds));
        else
            curCache.Put(Key, value, new TimeSpan(0, 0, timeoutInSeconds));
    }

And it is duely giving the notifications for all operations. The notification for expiration will come only after the item is evicted from memory not when the TTL (time to live) is over. I have confirmed this from Microsoft. you should wait a bit to get notifications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文