Velocity 是否支持服务器端原子更新?我正在尝试查看是否可以移植一些代码(基于 memcached),这些代码实现了基于 memcache 的 INCR 操作的环形缓冲区。
问问题
318 次
1 回答
3
我不能说我对 memcached 足够熟悉,可以确切地知道你的意思,但我假设它涉及锁定一个缓存的项目,以便一个客户端可以更新它,这由 Velocity 通过GetAndLock和PutAndUnlock 支持方法。
编辑:好的,现在我明白你的意思了,不,我没有在 Velocity 中看到过类似的东西。但是您可以将其编写为扩展方法,例如
Imports System.Runtime.CompilerServices
Public Module VelocityExtensions
<Extension()> _
Public Sub Increment(ByVal cache As Microsoft.Data.Caching.DataCache, ByVal itemKey As String)
Dim cachedInteger As Integer
Dim cacheLockHandle As DataCacheLockHandle
cachedInteger = DirectCast(cache.GetAndLock(itemKey, New TimeSpan(0, 0, 5), cacheLockHandle), Integer)
cachedInteger += 1
cache.PutAndUnlock(itemKey, cachedInteger, cacheLockHandle)
End Sub
<Extension()> _
Public Sub Decrement(ByVal cache As Microsoft.Data.Caching.DataCache, ByVal itemKey As String)
Dim cachedInteger As Integer
Dim cacheLockHandle As DataCacheLockHandle
cachedInteger = DirectCast(cache.GetAndLock(itemKey, New TimeSpan(0, 0, 5), cacheLockHandle), Integer)
cachedInteger -= 1
cache.PutAndUnlock(itemKey, cachedInteger, cacheLockHandle)
End Sub
End Module
您的用法将变为:
Imports VelocityExtensions
Imports Microsoft.Data.Caching
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myCache As DataCache
Dim factory As DataCacheFactory
myCache = factory.GetCache("MyCacheName")
myCache.Increment("MyInteger")
End Sub
End Class
于 2009-10-14T14:38:52.397 回答