2019-05-17 08:40:41 +00:00
package common
import (
2022-02-21 03:32:53 +00:00
//"log"
"sync"
"sync/atomic"
2019-05-17 08:40:41 +00:00
)
// ReplyCache is an interface which spits out replies from a fast cache rather than the database, whether from memory or from an application like Redis. Replies may not be present in the cache but may be in the database
type ReplyCache interface {
2022-02-21 03:32:53 +00:00
Get ( id int ) ( * Reply , error )
GetUnsafe ( id int ) ( * Reply , error )
BulkGet ( ids [ ] int ) ( list [ ] * Reply )
Set ( item * Reply ) error
Add ( item * Reply ) error
AddUnsafe ( item * Reply ) error
Remove ( id int ) error
RemoveUnsafe ( id int ) error
Flush ( )
Length ( ) int
SetCapacity ( cap int )
GetCapacity ( ) int
2019-05-17 08:40:41 +00:00
}
// MemoryReplyCache stores and pulls replies out of the current process' memory
type MemoryReplyCache struct {
2022-02-21 03:32:53 +00:00
items map [ int ] * Reply
length int64 // sync/atomic only lets us operate on int32s and int64s
capacity int
2019-05-17 08:40:41 +00:00
2022-02-21 03:32:53 +00:00
sync . RWMutex
2019-05-17 08:40:41 +00:00
}
// NewMemoryReplyCache gives you a new instance of MemoryReplyCache
2020-02-05 02:48:35 +00:00
func NewMemoryReplyCache ( cap int ) * MemoryReplyCache {
2022-02-21 03:32:53 +00:00
return & MemoryReplyCache {
items : make ( map [ int ] * Reply ) ,
capacity : cap ,
}
2019-05-17 08:40:41 +00:00
}
// Get fetches a reply by ID. Returns ErrNoRows if not present.
2019-08-31 22:07:34 +00:00
func ( s * MemoryReplyCache ) Get ( id int ) ( * Reply , error ) {
2022-02-21 03:32:53 +00:00
s . RLock ( )
item , ok := s . items [ id ]
s . RUnlock ( )
if ok {
return item , nil
}
return item , ErrNoRows
2019-05-17 08:40:41 +00:00
}
// GetUnsafe fetches a reply by ID. Returns ErrNoRows if not present. THIS METHOD IS NOT THREAD-SAFE.
2019-08-31 22:07:34 +00:00
func ( s * MemoryReplyCache ) GetUnsafe ( id int ) ( * Reply , error ) {
2022-02-21 03:32:53 +00:00
item , ok := s . items [ id ]
if ok {
return item , nil
}
return item , ErrNoRows
2019-05-17 08:40:41 +00:00
}
// BulkGet fetches multiple replies by their IDs. Indices without replies will be set to nil, so make sure you check for those, we might want to change this behaviour to make it less confusing.
2019-08-31 22:07:34 +00:00
func ( s * MemoryReplyCache ) BulkGet ( ids [ ] int ) ( list [ ] * Reply ) {
2022-02-21 03:32:53 +00:00
list = make ( [ ] * Reply , len ( ids ) )
s . RLock ( )
for i , id := range ids {
list [ i ] = s . items [ id ]
}
s . RUnlock ( )
return list
2019-05-17 08:40:41 +00:00
}
// Set overwrites the value of a reply in the cache, whether it's present or not. May return a capacity overflow error.
2019-08-31 22:07:34 +00:00
func ( s * MemoryReplyCache ) Set ( item * Reply ) error {
2022-02-21 03:32:53 +00:00
s . Lock ( )
_ , ok := s . items [ item . ID ]
if ok {
s . items [ item . ID ] = item
} else if int ( s . length ) >= s . capacity {
s . Unlock ( )
return ErrStoreCapacityOverflow
} else {
s . items [ item . ID ] = item
atomic . AddInt64 ( & s . length , 1 )
}
s . Unlock ( )
return nil
2019-05-17 08:40:41 +00:00
}
// Add adds a reply to the cache, similar to Set, but it's only intended for new items. This method might be deprecated in the near future, use Set. May return a capacity overflow error.
// ? Is this redundant if we have Set? Are the efficiency wins worth this? Is this even used?
2019-08-31 22:07:34 +00:00
func ( s * MemoryReplyCache ) Add ( item * Reply ) error {
2022-02-21 03:32:53 +00:00
//log.Print("MemoryReplyCache.Add")
s . Lock ( )
if int ( s . length ) >= s . capacity {
s . Unlock ( )
return ErrStoreCapacityOverflow
}
s . items [ item . ID ] = item
s . Unlock ( )
atomic . AddInt64 ( & s . length , 1 )
return nil
2019-05-17 08:40:41 +00:00
}
// AddUnsafe is the unsafe version of Add. May return a capacity overflow error. THIS METHOD IS NOT THREAD-SAFE.
2019-08-31 22:07:34 +00:00
func ( s * MemoryReplyCache ) AddUnsafe ( item * Reply ) error {
2022-02-21 03:32:53 +00:00
if int ( s . length ) >= s . capacity {
return ErrStoreCapacityOverflow
}
s . items [ item . ID ] = item
s . length = int64 ( len ( s . items ) )
return nil
2019-05-17 08:40:41 +00:00
}
// Remove removes a reply from the cache by ID, if they exist. Returns ErrNoRows if no items exist.
2019-08-31 22:07:34 +00:00
func ( s * MemoryReplyCache ) Remove ( id int ) error {
2022-02-21 03:32:53 +00:00
s . Lock ( )
_ , ok := s . items [ id ]
if ! ok {
s . Unlock ( )
return ErrNoRows
}
delete ( s . items , id )
s . Unlock ( )
atomic . AddInt64 ( & s . length , - 1 )
return nil
2019-05-17 08:40:41 +00:00
}
// RemoveUnsafe is the unsafe version of Remove. THIS METHOD IS NOT THREAD-SAFE.
func ( s * MemoryReplyCache ) RemoveUnsafe ( id int ) error {
2022-02-21 03:32:53 +00:00
_ , ok := s . items [ id ]
if ! ok {
return ErrNoRows
}
delete ( s . items , id )
atomic . AddInt64 ( & s . length , - 1 )
return nil
2019-05-17 08:40:41 +00:00
}
// Flush removes all the replies from the cache, useful for tests.
func ( s * MemoryReplyCache ) Flush ( ) {
2022-02-21 03:32:53 +00:00
s . Lock ( )
s . items = make ( map [ int ] * Reply )
s . length = 0
s . Unlock ( )
2019-05-17 08:40:41 +00:00
}
// ! Is this concurrent?
// Length returns the number of replies in the memory cache
func ( s * MemoryReplyCache ) Length ( ) int {
2022-02-21 03:32:53 +00:00
return int ( s . length )
2019-05-17 08:40:41 +00:00
}
// SetCapacity sets the maximum number of replies which this cache can hold
2020-02-05 02:48:35 +00:00
func ( s * MemoryReplyCache ) SetCapacity ( cap int ) {
2022-02-21 03:32:53 +00:00
// Ints are moved in a single instruction, so this should be thread-safe
s . capacity = cap
2019-05-17 08:40:41 +00:00
}
// GetCapacity returns the maximum number of replies this cache can hold
func ( s * MemoryReplyCache ) GetCapacity ( ) int {
2022-02-21 03:32:53 +00:00
return s . capacity
2019-05-17 08:40:41 +00:00
}