Documentation ¶
Overview ¶
Copyright 2017 Mailgun Technologies Inc
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Modifications Copyright 2017 Mailgun Technologies Inc ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
This work is derived from github.com/golang/groupcache/lru
Copyright 2017 Mailgun Technologies Inc ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2017 Mailgun Technologies Inc ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Index ¶
- type CacheItem
- type ExpireCache
- func (c *ExpireCache) Add(key, value interface{})
- func (c *ExpireCache) Each(concurrent int, callBack func(key interface{}, value interface{}) error) []error
- func (c *ExpireCache) Get(key interface{}) (interface{}, bool)
- func (c *ExpireCache) GetStats() ExpireCacheStats
- func (c *ExpireCache) Keys() (keys []interface{})
- func (c *ExpireCache) Peek(key interface{}) (value interface{}, ok bool)
- func (c *ExpireCache) Size() int64
- func (c *ExpireCache) Update(key, value interface{}) error
- type ExpireCacheStats
- type Key
- type LRUCache
- func (c *LRUCache) Add(key Key, value interface{}) bool
- func (c *LRUCache) AddWithTTL(key Key, value interface{}, ttl clock.Duration) bool
- func (c *LRUCache) Each(concurrent int, callBack func(key interface{}, value interface{}) error) []error
- func (c *LRUCache) Get(key Key) (value interface{}, ok bool)
- func (c *LRUCache) Keys() (keys []interface{})
- func (c *LRUCache) Map(mapping func(item *CacheItem) bool)
- func (c *LRUCache) Peek(key interface{}) (value interface{}, ok bool)
- func (c *LRUCache) Remove(key Key)
- func (c *LRUCache) Size() int
- func (c *LRUCache) Stats() LRUCacheStats
- type LRUCacheStats
- type PQItem
- type PriorityQueue
- type TTLMap
- func (m *TTLMap) Get(key string) (interface{}, bool)
- func (m *TTLMap) GetInt(key string) (retval int, found bool, reterr error)
- func (m *TTLMap) Increment(key string, value, ttlSeconds int) (retval int, reterr error)
- func (m *TTLMap) Len() int
- func (m *TTLMap) RemoveExpired(iterations int) int
- func (m *TTLMap) RemoveLastUsed(iterations int)
- func (m *TTLMap) Set(key string, value interface{}, ttlSeconds int) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ExpireCache ¶
type ExpireCache struct {
// contains filtered or unexported fields
}
ExpireCache is a cache which expires entries only after 2 conditions are met 1. The Specified TTL has expired 2. The item has been processed with ExpireCache.Each()
This is an unbounded cache which guaranties each item in the cache has been processed before removal. This is different from a LRU cache, as the cache might decide an item needs to be removed (because we hit the cache limit) before the item has been processed.
Every time an item is touched by `Get()` or `Add()` the duration is updated which ensures items in frequent use stay in the cache
Processing can modify the item in the cache without updating the expiration time by using the `Update()` method
The cache can also return statistics which can be used to graph track the size of the cache
NOTE: Because this is an unbounded cache, the user MUST process the cache with `Each()` regularly! Else the cache items will never expire and the cache will eventually eat all the memory on the system
func NewExpireCache ¶
func NewExpireCache(ttl clock.Duration) *ExpireCache
New creates a new ExpireCache.
func (*ExpireCache) Add ¶
func (c *ExpireCache) Add(key, value interface{})
Put the key, value and TTL in the cache
func (*ExpireCache) Each ¶
func (c *ExpireCache) Each(concurrent int, callBack func(key interface{}, value interface{}) error) []error
Processes each item in the cache in a thread safe way, such that the cache can be in use while processing items in the cache
func (*ExpireCache) Get ¶
func (c *ExpireCache) Get(key interface{}) (interface{}, bool)
Retrieves a key's value from the cache
func (*ExpireCache) GetStats ¶
func (c *ExpireCache) GetStats() ExpireCacheStats
Retrieve stats about the cache
func (*ExpireCache) Keys ¶
func (c *ExpireCache) Keys() (keys []interface{})
Get a list of keys at this point in time
func (*ExpireCache) Peek ¶
func (c *ExpireCache) Peek(key interface{}) (value interface{}, ok bool)
Get the value without updating the expiration
func (*ExpireCache) Size ¶
func (c *ExpireCache) Size() int64
Returns the number of items in the cache.
func (*ExpireCache) Update ¶
func (c *ExpireCache) Update(key, value interface{}) error
Update the value in the cache without updating the TTL
type ExpireCacheStats ¶
type Key ¶
type Key interface{}
A Key may be any value that is comparable. See http://golang.org/ref/spec#Comparison_operators
type LRUCache ¶
type LRUCache struct { // MaxEntries is the maximum number of cache entries before // an item is evicted. Zero means no limit. MaxEntries int // OnEvicted optionally specifies a callback function to be // executed when an entry is purged from the cache. OnEvicted func(key Key, value interface{}) // contains filtered or unexported fields }
Cache is an thread safe LRU cache that also supports optional TTL expiration You can use an non thread safe version of this
func NewLRUCache ¶
New creates a new Cache. If maxEntries is zero, the cache has no limit and it's assumed that eviction is done by the caller.
func (*LRUCache) AddWithTTL ¶
Adds a value to the cache with a TTL
func (*LRUCache) Each ¶
func (c *LRUCache) Each(concurrent int, callBack func(key interface{}, value interface{}) error) []error
Processes each item in the cache in a thread safe way, such that the cache can be in use while processing items in the cache. Processing the cache with `Each()` does not update the expiration or last used.
func (*LRUCache) Keys ¶
func (c *LRUCache) Keys() (keys []interface{})
Get a list of keys at this point in time
func (*LRUCache) Map ¶
Map modifies the cache according to the mapping function, If mapping returns false the item is removed from the cache and `OnEvicted` is called if defined. Map claims exclusive access to the cache; as such concurrent access will block until Map returns.
func (*LRUCache) Stats ¶
func (c *LRUCache) Stats() LRUCacheStats
Returns stats about the current state of the cache
type LRUCacheStats ¶
Holds stats collected about the cache
type PQItem ¶
type PQItem struct { Value interface{} Priority int // The priority of the item in the queue. // contains filtered or unexported fields }
An PQItem is something we manage in a priority queue.
type PriorityQueue ¶
type PriorityQueue struct {
// contains filtered or unexported fields
}
Implements a PriorityQueue
func NewPriorityQueue ¶
func NewPriorityQueue() *PriorityQueue
Example ¶
package main import ( "fmt" "github.com/mailgun/holster/v4/collections" ) func main() { queue := collections.NewPriorityQueue() queue.Push(&collections.PQItem{ Value: "thing3", Priority: 3, }) queue.Push(&collections.PQItem{ Value: "thing1", Priority: 1, }) queue.Push(&collections.PQItem{ Value: "thing2", Priority: 2, }) // Pops item off the queue according to the priority instead of the Push() order item := queue.Pop() fmt.Printf("Item: %s", item.Value.(string)) }
Output: Item: thing1
func (PriorityQueue) Len ¶
func (p PriorityQueue) Len() int
func (*PriorityQueue) Peek ¶
func (p *PriorityQueue) Peek() *PQItem
func (*PriorityQueue) Pop ¶
func (p *PriorityQueue) Pop() *PQItem
func (*PriorityQueue) Push ¶
func (p *PriorityQueue) Push(el *PQItem)
func (*PriorityQueue) Remove ¶
func (p *PriorityQueue) Remove(el *PQItem)
func (*PriorityQueue) Update ¶
func (p *PriorityQueue) Update(el *PQItem, priority int)
Modifies the priority and value of an Item in the queue.
type TTLMap ¶
type TTLMap struct { // Optionally specifies a callback function to be // executed when an entry has expired OnExpire func(key string, i interface{}) // contains filtered or unexported fields }