Velocity是否支持服务器端原子更新? 我正在尝试查看是否可以移植一些基于memcache的INCR操作实现环形缓冲区的代码(基于memcached)。
答案 0 :(得分: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