<commit_before>/*
 * 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.
 */

package memory

import (
	"sync"
	"time"
)

var zeroExpiration = time.Duration(0)

// Intelligent holds the required variables to compose an in memory cache system
// which also provides expiring key mechanism and also maxSize
type Intelligent struct {
	// Mutex is used for handling the concurrent
	// read/write requests for cache
	sync.Mutex

	// items hold the cached objects
	items map[string]interface{}

	// createdAt holds the time that related item's created At
	createdAt map[string]time.Time

	// expiration is a duration for a cache key to expire
	expiration time.Duration

	// gcInterval is a duration for garbage collection
	gcInterval time.Duration

	// maxSize is a total size for overall cache
	maxSize uint64

	// currentSize is a current size in memory
	currentSize uint64

	// OnEvicted - callback function for eviction
	OnEvicted func(a ...interface{})

	// totalEvicted counter to keep track of total evictions
	totalEvicted uint64
}

// Stats current cache statistics
type Stats struct {
	Bytes     uint64
	Items     uint64
	Evictions uint64
}

// NewIntelligent creates an inmemory cache
//
// maxSize is used for expiring objects before we run out of memory
// expiration is used for expiration of a key from cache
func NewIntelligent(maxSize uint64, expiration time.Duration) *Intelligent {
	return &Intelligent{
		items:      map[string]interface{}{},
		createdAt:  map[string]time.Time{},
		expiration: expiration,
		maxSize:    maxSize,
	}
}

// Stats get current cache statistics
func (r *Intelligent) Stats() Stats {
	return Stats{
		Bytes:     r.currentSize,
		Items:     uint64(len(r.items)),
		Evictions: r.totalEvicted,
	}
}

// ExpireObjects expire objects in go routine
func (r *Intelligent) ExpireObjects(gcInterval time.Duration) {
	r.gcInterval = gcInterval
	go func() {
		for range time.Tick(gcInterval) {
			for key := range r.items {
				r.Lock()
				if !r.isValid(key) {
					r.Delete(key)
				}
				r.Unlock()
			}
		}
	}()
}

// Get returns a value of a given key if it exists
func (r *Intelligent) Get(key string) (interface{}, bool) {
	r.Lock()
	defer r.Unlock()
	value, ok := r.items[key]<commit_msg>Fix a potential race between ExpireObjects() and Set() over items map<commit_after>/*
 * 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.
 */

package memory

import (
	"sync"
	"time"
)

var zeroExpiration = time.Duration(0)

// Intelligent holds the required variables to compose an in memory cache system
// which also provides expiring key mechanism and also maxSize
type Intelligent struct {
	// Mutex is used for handling the concurrent
	// read/write requests for cache
	sync.Mutex

	// items hold the cached objects
	items map[string]interface{}

	// createdAt holds the time that related item's created At
	createdAt map[string]time.Time

	// expiration is a duration for a cache key to expire
	expiration time.Duration

	// gcInterval is a duration for garbage collection
	gcInterval time.Duration

	// maxSize is a total size for overall cache
	maxSize uint64

	// currentSize is a current size in memory
	currentSize uint64

	// OnEvicted - callback function for eviction
	OnEvicted func(a ...interface{})

	// totalEvicted counter to keep track of total evictions
	totalEvicted uint64
}

// Stats current cache statistics
type Stats struct {
	Bytes     uint64
	Items     uint64
	Evictions uint64
}

// NewIntelligent creates an inmemory cache
//
// maxSize is used for expiring objects before we run out of memory
// expiration is used for expiration of a key from cache
func NewIntelligent(maxSize uint64, expiration time.Duration) *Intelligent {
	return &Intelligent{
		items:      map[string]interface{}{},
		createdAt:  map[string]time.Time{},
		expiration: expiration,
		maxSize:    maxSize,
	}
}

// Stats get current cache statistics
func (r *Intelligent) Stats() Stats {
	return Stats{
		Bytes:     r.currentSize,
		Items:     uint64(len(r.items)),
		Evictions: r.totalEvicted,
	}
}

// ExpireObjects expire objects in go routine
func (r *Intelligent) ExpireObjects(gcInterval time.Duration) {
	r.gcInterval = gcInterval
	go func() {
		for range time.Tick(gcInterval) {
			r.Lock()
			for key := range r.items {

				if !r.isValid(key) {
					r.Delete(key)
				}
			}
			r.Unlock()
		}
	}()
}

// Get returns a value of a given key if it exists
func (r *Intelligent) Get(key string) (interface{}, bool) {
	r.Lock()
	defer r.Unlock()
	value, ok := r.items[key]
{'fraction_non_alphanumeric': 0.08545486914723162, 'fraction_numerical': 0.008901548869503293, 'mean_word_length': 3.552674230145867, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5556284', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1613, 'n_words': 671}