2019-05-17 08:40:41 +00:00
package common
import (
2020-01-14 05:07:00 +00:00
//"log"
2019-05-17 08:40:41 +00:00
"sync"
"sync/atomic"
)
// 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 {
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 ( capacity int )
GetCapacity ( ) int
}
// MemoryReplyCache stores and pulls replies out of the current process' memory
type MemoryReplyCache struct {
items map [ int ] * Reply
length int64 // sync/atomic only lets us operate on int32s and int64s
capacity int
sync . RWMutex
}
// NewMemoryReplyCache gives you a new instance of MemoryReplyCache
func NewMemoryReplyCache ( capacity int ) * MemoryReplyCache {
return & MemoryReplyCache {
items : make ( map [ int ] * Reply ) ,
capacity : capacity ,
}
}
// 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 ) {
s . RLock ( )
item , ok := s . items [ id ]
s . RUnlock ( )
2019-05-17 08:40:41 +00:00
if ok {
return item , nil
}
return item , ErrNoRows
}
// 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 ) {
item , ok := s . items [ id ]
2019-05-17 08:40:41 +00:00
if ok {
return item , nil
}
return item , ErrNoRows
}
// 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 ) {
2019-05-17 08:40:41 +00:00
list = make ( [ ] * Reply , len ( ids ) )
2019-08-31 22:07:34 +00:00
s . RLock ( )
2019-05-17 08:40:41 +00:00
for i , id := range ids {
2019-08-31 22:07:34 +00:00
list [ i ] = s . items [ id ]
2019-05-17 08:40:41 +00:00
}
2019-08-31 22:07:34 +00:00
s . RUnlock ( )
2019-05-17 08:40:41 +00:00
return list
}
// 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 {
s . Lock ( )
_ , ok := s . items [ item . ID ]
2019-05-17 08:40:41 +00:00
if ok {
2019-08-31 22:07:34 +00:00
s . items [ item . ID ] = item
} else if int ( s . length ) >= s . capacity {
s . Unlock ( )
2019-05-17 08:40:41 +00:00
return ErrStoreCapacityOverflow
} else {
2019-08-31 22:07:34 +00:00
s . items [ item . ID ] = item
atomic . AddInt64 ( & s . length , 1 )
2019-05-17 08:40:41 +00:00
}
2019-08-31 22:07:34 +00:00
s . Unlock ( )
2019-05-17 08:40:41 +00:00
return nil
}
// 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 {
2020-01-14 05:07:00 +00:00
//log.Print("MemoryReplyCache.Add")
2019-08-31 22:07:34 +00:00
s . Lock ( )
if int ( s . length ) >= s . capacity {
s . Unlock ( )
2019-05-17 08:40:41 +00:00
return ErrStoreCapacityOverflow
}
2019-08-31 22:07:34 +00:00
s . items [ item . ID ] = item
s . Unlock ( )
atomic . AddInt64 ( & s . length , 1 )
2019-05-17 08:40:41 +00:00
return nil
}
// 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 {
if int ( s . length ) >= s . capacity {
2019-05-17 08:40:41 +00:00
return ErrStoreCapacityOverflow
}
2019-08-31 22:07:34 +00:00
s . items [ item . ID ] = item
s . length = int64 ( len ( s . items ) )
2019-05-17 08:40:41 +00:00
return nil
}
// 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 {
s . Lock ( )
_ , ok := s . items [ id ]
2019-05-17 08:40:41 +00:00
if ! ok {
2019-08-31 22:07:34 +00:00
s . Unlock ( )
2019-05-17 08:40:41 +00:00
return ErrNoRows
}
2019-08-31 22:07:34 +00:00
delete ( s . items , id )
s . Unlock ( )
atomic . AddInt64 ( & s . length , - 1 )
2019-05-17 08:40:41 +00:00
return nil
}
// RemoveUnsafe is the unsafe version of Remove. THIS METHOD IS NOT THREAD-SAFE.
func ( s * MemoryReplyCache ) RemoveUnsafe ( id int ) error {
_ , ok := s . items [ id ]
if ! ok {
return ErrNoRows
}
delete ( s . items , id )
atomic . AddInt64 ( & s . length , - 1 )
return nil
}
// Flush removes all the replies from the cache, useful for tests.
func ( s * MemoryReplyCache ) Flush ( ) {
s . Lock ( )
s . items = make ( map [ int ] * Reply )
s . length = 0
s . Unlock ( )
}
// ! Is this concurrent?
// Length returns the number of replies in the memory cache
func ( s * MemoryReplyCache ) Length ( ) int {
return int ( s . length )
}
// SetCapacity sets the maximum number of replies which this cache can hold
func ( s * MemoryReplyCache ) SetCapacity ( capacity int ) {
// Ints are moved in a single instruction, so this should be thread-safe
s . capacity = capacity
}
// GetCapacity returns the maximum number of replies this cache can hold
func ( s * MemoryReplyCache ) GetCapacity ( ) int {
return s . capacity
}