2009-10-13 2 views
0

Velocity는 서버 측 원자 업데이트를 지원합니까? Memcache의 INCR 작업을 기반으로 링 버퍼를 구현 한 일부 코드 (memcached 기반)를 포팅 할 수 있는지 확인하려고합니다.MS 속도의 원자 업데이트

답변

3

내가 memcached와 충분히 친숙하다는 것을 알 수는 없다. 정확하게는 무엇을 의미 하나 캐시 된 항목을 잠궈 서 하나의 클라이언트가이를 지원할 수 있다고 가정하고있다. 속도는 GetAndLockPutAndUnlock 방법을 통해 이루어집니다.

편집 : 좋아, 이제 무슨 뜻인지 알 겠어. 벨로 시티에서 그런 건 본적이 없어. 하지만 확장 메소드로 작성할 수 있습니다.

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 

귀하의 사용은 될 것입니다 : memcached를에서

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 
+0

당신은 단일 서버 왕복에 원자 증가 및 감소 할 수 있습니다. 예를 들어, 한 번에 서버에서 잠금/증가/잠금 해제를 수행하는 client.Increment ("totalViews-"+ contentId)를 수행 할 수 있습니다. – JBland

+0

내 대답을 업데이트하여 어떻게 할 수 있는지 보여주십시오. – PhilPursglove

+0

고마워, 필. 확실히 효과적이지 만, ID만큼 효율적이지는 않습니다. – JBland

관련 문제