You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
183 lines
4.1 KiB
183 lines
4.1 KiB
10 years ago
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
10 years ago
|
// Package trove implements in memory caching methods
|
||
|
package trove
|
||
10 years ago
|
|
||
|
import (
|
||
|
"sync"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
10 years ago
|
var noExpiration = time.Duration(0)
|
||
10 years ago
|
|
||
10 years ago
|
// Cache holds the required variables to compose an in memory cache system
|
||
10 years ago
|
// which also provides expiring key mechanism and also maxSize
|
||
10 years ago
|
type Cache struct {
|
||
10 years ago
|
// Mutex is used for handling the concurrent
|
||
|
// read/write requests for cache
|
||
|
sync.Mutex
|
||
|
|
||
|
// items hold the cached objects
|
||
10 years ago
|
items map[string][]byte
|
||
10 years ago
|
|
||
10 years ago
|
// updatedAt holds the time that related item's updated at
|
||
|
updatedAt map[string]time.Time
|
||
10 years ago
|
|
||
|
// expiration is a duration for a cache key to expire
|
||
|
expiration time.Duration
|
||
|
|
||
10 years ago
|
// stopExpireTimer channel to quit the timer thread
|
||
|
stopExpireTimer chan struct{}
|
||
10 years ago
|
|
||
|
// maxSize is a total size for overall cache
|
||
|
maxSize uint64
|
||
|
|
||
|
// currentSize is a current size in memory
|
||
|
currentSize uint64
|
||
|
|
||
10 years ago
|
// OnExpired - callback function for eviction
|
||
|
OnExpired func(a ...interface{})
|
||
10 years ago
|
|
||
10 years ago
|
// totalExpired counter to keep track of total expirations
|
||
|
totalExpired uint64
|
||
10 years ago
|
}
|
||
|
|
||
|
// Stats current cache statistics
|
||
|
type Stats struct {
|
||
10 years ago
|
Bytes uint64
|
||
|
Items uint64
|
||
|
Expired uint64
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// NewCache creates an inmemory cache
|
||
10 years ago
|
//
|
||
|
// maxSize is used for expiring objects before we run out of memory
|
||
|
// expiration is used for expiration of a key from cache
|
||
10 years ago
|
func NewCache(maxSize uint64, expiration time.Duration) *Cache {
|
||
|
return &Cache{
|
||
10 years ago
|
items: make(map[string][]byte),
|
||
10 years ago
|
updatedAt: map[string]time.Time{},
|
||
10 years ago
|
expiration: expiration,
|
||
|
maxSize: maxSize,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Stats get current cache statistics
|
||
10 years ago
|
func (r *Cache) Stats() Stats {
|
||
10 years ago
|
return Stats{
|
||
10 years ago
|
Bytes: r.currentSize,
|
||
|
Items: uint64(len(r.items)),
|
||
|
Expired: r.totalExpired,
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// ExpireObjects expire objects in go routine
|
||
10 years ago
|
func (r *Cache) ExpireObjects(gcInterval time.Duration) {
|
||
10 years ago
|
r.stopExpireTimer = make(chan struct{})
|
||
|
ticker := time.NewTicker(gcInterval)
|
||
10 years ago
|
go func() {
|
||
10 years ago
|
for {
|
||
|
select {
|
||
|
case <-ticker.C:
|
||
|
r.Expire()
|
||
|
case <-r.stopExpireTimer:
|
||
|
ticker.Stop()
|
||
|
return
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
}
|
||
|
}()
|
||
|
}
|
||
|
|
||
|
// Get returns a value of a given key if it exists
|
||
10 years ago
|
func (r *Cache) Get(key string) ([]byte, bool) {
|
||
10 years ago
|
r.Lock()
|
||
|
defer r.Unlock()
|
||
|
value, ok := r.items[key]
|
||
10 years ago
|
if !ok {
|
||
|
return nil, false
|
||
|
}
|
||
|
r.updatedAt[key] = time.Now()
|
||
|
return value, true
|
||
10 years ago
|
}
|
||
|
|
||
|
// Set will persist a value to the cache
|
||
10 years ago
|
func (r *Cache) Set(key string, value []byte) bool {
|
||
10 years ago
|
r.Lock()
|
||
10 years ago
|
defer r.Unlock()
|
||
10 years ago
|
valueLen := uint64(len(value))
|
||
10 years ago
|
if r.maxSize > 0 {
|
||
10 years ago
|
// 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
|
||
10 years ago
|
for key := range r.items {
|
||
10 years ago
|
for (r.currentSize + valueLen) > r.maxSize {
|
||
10 years ago
|
r.doDelete(key)
|
||
10 years ago
|
}
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
r.items[key] = value
|
||
10 years ago
|
r.currentSize += valueLen
|
||
10 years ago
|
r.updatedAt[key] = time.Now()
|
||
10 years ago
|
return true
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// Expire expires keys which have expired
|
||
10 years ago
|
func (r *Cache) Expire() {
|
||
10 years ago
|
r.Lock()
|
||
|
defer r.Unlock()
|
||
|
for key := range r.items {
|
||
|
if !r.isValid(key) {
|
||
10 years ago
|
r.doDelete(key)
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
// Delete deletes a given key if exists
|
||
10 years ago
|
func (r *Cache) Delete(key string) {
|
||
10 years ago
|
r.Lock()
|
||
|
defer r.Unlock()
|
||
|
r.doDelete(key)
|
||
|
}
|
||
|
|
||
|
func (r *Cache) doDelete(key string) {
|
||
10 years ago
|
if _, ok := r.items[key]; ok {
|
||
10 years ago
|
r.currentSize -= uint64(len(r.items[key]))
|
||
10 years ago
|
delete(r.items, key)
|
||
|
delete(r.updatedAt, key)
|
||
10 years ago
|
r.totalExpired++
|
||
|
if r.OnExpired != nil {
|
||
|
r.OnExpired(key)
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
func (r *Cache) isValid(key string) bool {
|
||
10 years ago
|
updatedAt, ok := r.updatedAt[key]
|
||
10 years ago
|
if !ok {
|
||
|
return false
|
||
|
}
|
||
10 years ago
|
if r.expiration == noExpiration {
|
||
10 years ago
|
return true
|
||
|
}
|
||
10 years ago
|
return updatedAt.Add(r.expiration).After(time.Now())
|
||
10 years ago
|
}
|