2015-05-04 22:04:56 -04:00
|
|
|
/*
|
|
|
|
* Minimalist Object Storage, (C) 2015 Minio, 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.
|
|
|
|
*/
|
|
|
|
|
2015-07-04 00:02:31 -04:00
|
|
|
// Package data implements in memory caching methods for data
|
|
|
|
package data
|
2015-05-04 22:04:56 -04:00
|
|
|
|
|
|
|
import (
|
2015-07-04 20:08:23 -04:00
|
|
|
"container/list"
|
2015-05-04 22:04:56 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-05-31 21:38:34 -04:00
|
|
|
var noExpiration = time.Duration(0)
|
2015-05-04 22:04:56 -04:00
|
|
|
|
2015-05-14 02:05:48 -04:00
|
|
|
// Cache holds the required variables to compose an in memory cache system
|
2015-05-04 22:04:56 -04:00
|
|
|
// which also provides expiring key mechanism and also maxSize
|
2015-05-14 02:05:48 -04:00
|
|
|
type Cache struct {
|
2015-05-04 22:04:56 -04:00
|
|
|
// Mutex is used for handling the concurrent
|
|
|
|
// read/write requests for cache
|
|
|
|
sync.Mutex
|
|
|
|
|
|
|
|
// items hold the cached objects
|
2015-07-04 20:08:23 -04:00
|
|
|
items *list.List
|
2015-05-04 22:04:56 -04:00
|
|
|
|
2015-07-04 20:08:23 -04:00
|
|
|
// reverseItems holds the time that related item's updated at
|
2015-07-05 21:44:55 -04:00
|
|
|
reverseItems map[interface{}]*list.Element
|
2015-05-04 22:04:56 -04:00
|
|
|
|
|
|
|
// maxSize is a total size for overall cache
|
|
|
|
maxSize uint64
|
|
|
|
|
|
|
|
// currentSize is a current size in memory
|
|
|
|
currentSize uint64
|
|
|
|
|
2015-07-04 20:08:23 -04:00
|
|
|
// OnEvicted - callback function for eviction
|
|
|
|
OnEvicted func(a ...interface{})
|
2015-05-04 22:04:56 -04:00
|
|
|
|
2015-07-04 20:08:23 -04:00
|
|
|
// totalEvicted counter to keep track of total expirations
|
|
|
|
totalEvicted int
|
2015-05-04 22:04:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stats current cache statistics
|
|
|
|
type Stats struct {
|
2015-05-08 15:00:02 -04:00
|
|
|
Bytes uint64
|
2015-07-04 20:08:23 -04:00
|
|
|
Items int
|
|
|
|
Evicted int
|
|
|
|
}
|
|
|
|
|
|
|
|
type element struct {
|
2015-07-05 21:44:55 -04:00
|
|
|
key interface{}
|
2015-07-04 20:08:23 -04:00
|
|
|
value []byte
|
2015-05-04 22:04:56 -04:00
|
|
|
}
|
|
|
|
|
2015-05-14 02:05:48 -04:00
|
|
|
// NewCache creates an inmemory cache
|
2015-05-04 22:04:56 -04:00
|
|
|
//
|
|
|
|
// maxSize is used for expiring objects before we run out of memory
|
|
|
|
// expiration is used for expiration of a key from cache
|
2015-07-04 20:08:23 -04:00
|
|
|
func NewCache(maxSize uint64) *Cache {
|
2015-05-14 02:05:48 -04:00
|
|
|
return &Cache{
|
2015-07-04 20:08:23 -04:00
|
|
|
items: list.New(),
|
2015-07-05 21:44:55 -04:00
|
|
|
reverseItems: make(map[interface{}]*list.Element),
|
2015-07-04 20:08:23 -04:00
|
|
|
maxSize: maxSize,
|
2015-05-04 22:04:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 01:17:56 -04:00
|
|
|
// SetMaxSize set a new max size
|
|
|
|
func (r *Cache) SetMaxSize(maxSize uint64) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
r.maxSize = maxSize
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-04 22:04:56 -04:00
|
|
|
// Stats get current cache statistics
|
2015-05-14 02:05:48 -04:00
|
|
|
func (r *Cache) Stats() Stats {
|
2015-05-04 22:04:56 -04:00
|
|
|
return Stats{
|
2015-05-08 15:00:02 -04:00
|
|
|
Bytes: r.currentSize,
|
2015-07-04 20:08:23 -04:00
|
|
|
Items: r.items.Len(),
|
|
|
|
Evicted: r.totalEvicted,
|
2015-05-04 22:04:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns a value of a given key if it exists
|
2015-07-05 21:44:55 -04:00
|
|
|
func (r *Cache) Get(key interface{}) ([]byte, bool) {
|
2015-05-04 22:04:56 -04:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2015-07-04 20:08:23 -04:00
|
|
|
ele, hit := r.reverseItems[key]
|
|
|
|
if !hit {
|
2015-05-05 17:27:18 -04:00
|
|
|
return nil, false
|
|
|
|
}
|
2015-07-04 20:08:23 -04:00
|
|
|
r.items.MoveToFront(ele)
|
|
|
|
return ele.Value.(*element).value, true
|
2015-05-04 22:04:56 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 00:39:37 -04:00
|
|
|
// Len returns length of the value of a given key, returns zero if key doesn't exist
|
2015-07-05 21:44:55 -04:00
|
|
|
func (r *Cache) Len(key interface{}) int {
|
2015-07-02 00:39:37 -04:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2015-07-04 20:08:23 -04:00
|
|
|
_, ok := r.reverseItems[key]
|
2015-07-02 00:39:37 -04:00
|
|
|
if !ok {
|
|
|
|
return 0
|
|
|
|
}
|
2015-07-04 20:08:23 -04:00
|
|
|
return len(r.reverseItems[key].Value.(*element).value)
|
2015-07-02 00:39:37 -04:00
|
|
|
}
|
|
|
|
|
2015-06-27 22:52:22 -04:00
|
|
|
// Append will append new data to an existing key,
|
|
|
|
// if key doesn't exist it behaves like Set()
|
2015-07-05 21:44:55 -04:00
|
|
|
func (r *Cache) Append(key interface{}, value []byte) bool {
|
2015-06-27 22:52:22 -04:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
valueLen := uint64(len(value))
|
|
|
|
if r.maxSize > 0 {
|
|
|
|
// check if the size of the object is not bigger than the
|
|
|
|
// capacity of the cache
|
|
|
|
if valueLen > r.maxSize {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// remove random key if only we reach the maxSize threshold
|
|
|
|
for (r.currentSize + valueLen) > r.maxSize {
|
2015-07-04 20:08:23 -04:00
|
|
|
r.doDeleteOldest()
|
|
|
|
break
|
2015-06-27 22:52:22 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-04 20:08:23 -04:00
|
|
|
ele, hit := r.reverseItems[key]
|
|
|
|
if !hit {
|
|
|
|
ele := r.items.PushFront(&element{key, value})
|
2015-06-27 22:52:22 -04:00
|
|
|
r.currentSize += valueLen
|
2015-07-04 20:08:23 -04:00
|
|
|
r.reverseItems[key] = ele
|
2015-06-27 22:52:22 -04:00
|
|
|
return true
|
|
|
|
}
|
2015-07-04 20:08:23 -04:00
|
|
|
r.items.MoveToFront(ele)
|
2015-06-27 22:52:22 -04:00
|
|
|
r.currentSize += valueLen
|
2015-07-04 20:08:23 -04:00
|
|
|
ele.Value.(*element).value = append(ele.Value.(*element).value, value...)
|
2015-06-27 22:52:22 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-05-04 22:04:56 -04:00
|
|
|
// Set will persist a value to the cache
|
2015-07-05 21:44:55 -04:00
|
|
|
func (r *Cache) Set(key interface{}, value []byte) bool {
|
2015-05-04 22:04:56 -04:00
|
|
|
r.Lock()
|
2015-05-28 14:33:35 -04:00
|
|
|
defer r.Unlock()
|
2015-05-31 21:38:34 -04:00
|
|
|
valueLen := uint64(len(value))
|
2015-05-04 22:04:56 -04:00
|
|
|
if r.maxSize > 0 {
|
2015-05-28 14:33:35 -04:00
|
|
|
// check if the size of the object is not bigger than the
|
|
|
|
// capacity of the cache
|
|
|
|
if valueLen > r.maxSize {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// remove random key if only we reach the maxSize threshold
|
2015-06-06 19:02:48 -04:00
|
|
|
for (r.currentSize + valueLen) > r.maxSize {
|
2015-07-04 20:08:23 -04:00
|
|
|
r.doDeleteOldest()
|
2015-05-04 22:04:56 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-04 20:08:23 -04:00
|
|
|
if _, hit := r.reverseItems[key]; hit {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ele := r.items.PushFront(&element{key, value})
|
2015-05-28 14:33:35 -04:00
|
|
|
r.currentSize += valueLen
|
2015-07-04 20:08:23 -04:00
|
|
|
r.reverseItems[key] = ele
|
2015-05-28 14:33:35 -04:00
|
|
|
return true
|
2015-05-04 22:04:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes a given key if exists
|
2015-07-05 21:44:55 -04:00
|
|
|
func (r *Cache) Delete(key interface{}) {
|
2015-05-31 14:35:34 -04:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2015-07-04 20:08:23 -04:00
|
|
|
ele, ok := r.reverseItems[key]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ele != nil {
|
|
|
|
r.currentSize -= uint64(len(r.reverseItems[key].Value.(*element).value))
|
|
|
|
r.items.Remove(ele)
|
|
|
|
delete(r.reverseItems, key)
|
|
|
|
r.totalEvicted++
|
|
|
|
if r.OnEvicted != nil {
|
|
|
|
r.OnEvicted(key)
|
2015-05-07 22:12:23 -04:00
|
|
|
}
|
2015-05-04 22:04:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-04 20:08:23 -04:00
|
|
|
func (r *Cache) doDeleteOldest() {
|
|
|
|
ele := r.items.Back()
|
|
|
|
if ele != nil {
|
|
|
|
r.currentSize -= uint64(len(r.reverseItems[ele.Value.(*element).key].Value.(*element).value))
|
|
|
|
delete(r.reverseItems, ele.Value.(*element).key)
|
|
|
|
r.items.Remove(ele)
|
|
|
|
r.totalEvicted++
|
|
|
|
if r.OnEvicted != nil {
|
|
|
|
r.OnEvicted(ele.Value.(*element).key)
|
|
|
|
}
|
2015-05-04 22:04:56 -04:00
|
|
|
}
|
|
|
|
}
|