lru

package
v0.3.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Copyright 2023 The acquirecloud Authors

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.

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 lru contains the container with limited size capacity and LRU (Least Recently Used) pull out discipline. The continer uses golang generics, so the container can be instantiated for differenty key and value types.

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.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] struct {
	*ECache[K, K, V]
}

Cache implements container with limited size capacity and LRU (Least Recently Used) pull out discipline. The elements can be created automatically if they are not found in the pool via the createNewF function call, which is provided via the Cache creation (see NewCache)

func NewCache

func NewCache[K comparable, V any](maxSize int, createNewF CreatePoolElemF[K, V], onDeleteF OnDeleteElemF[K, V]) (*Cache[K, V], error)

NewCache creates new pool object. It expects the maximum pull size (maxSize) and the create new element function in the parameters

type CacheItem

type CacheItem interface {
	GetValue() any
	GetExpiresAt() time.Time
}

CacheItem - interface for ExpirableItem that needs to be implemented in order to work with ExpirableCache cache

type CreatePoolElemF

type CreatePoolElemF[K any, V any] func(k K) (V, error)

CreatePoolElemF function type for creating new pool elements

type ECache

type ECache[PK any, K comparable, V any] struct {
	// contains filtered or unexported fields
}

ECache implements container with limited size capacity and LRU (Least Recently Used) pull out discipline. The elements can be created automatically if they are not found in the pool via the createNewF function call, which is provided via the Cache creation (see NewECache). ECache allows to operate with not comparable type as a primary key. For mapping the type to comparable on MapToInnerKeyF[] should be specified

func NewECache

func NewECache[PK any, K comparable, V any](maxSize int, toComparableF MapToInnerKeyF[PK, K], createNewF CreatePoolElemF[PK, V], onDeleteF OnDeleteElemF[PK, V]) (*ECache[PK, K, V], error)

NewECache creates new pool object. It expects the maximum pull size (maxSize) and the create new element function in the parameters

func (*ECache[PK, K, V]) Clear

func (p *ECache[PK, K, V]) Clear() int

Clear cleans up the cache removing all elements. The function will return number of the elements deleted

func (*ECache[PK, K, V]) GetOrCreate

func (p *ECache[PK, K, V]) GetOrCreate(pk PK) (V, error)

GetOrCreate returns an existing pool element or create the new one by its key

func (*ECache[PK, K, V]) Remove

func (p *ECache[PK, K, V]) Remove(pk PK) bool

Remove deletes the element by key k. It returns true if the element was in the collection and false if it was not found

type ExpirableCache

type ExpirableCache[K comparable, V CacheItem] struct {
	*Cache[K, V]
}

ExpirableCache - wrapper around cache that checks if value reached expires at timestamp and re-adds it to the cache by calling the createNewF

func NewExpirableCache

func NewExpirableCache[K comparable, V CacheItem](maxSize int, createNewF CreatePoolElemF[K, V], onDeleteF OnDeleteElemF[K, V]) (*ExpirableCache[K, V], error)

func (*ExpirableCache[K, V]) GetOrCreate

func (p *ExpirableCache[K, V]) GetOrCreate(k K) (V, error)

type ExpirableItem

type ExpirableItem[V any] struct {
	Value     V
	ExpiresAt time.Time
}

ExpirableItem - a helper struct for expirable cache. Allows to store any value in cache with a expires at timestamp.

func NewCacheItem

func NewCacheItem[V any](value V, expiresAt time.Time) ExpirableItem[V]

func (ExpirableItem[V]) GetExpiresAt

func (i ExpirableItem[V]) GetExpiresAt() time.Time

func (ExpirableItem[V]) GetValue

func (i ExpirableItem[V]) GetValue() any

type MapToInnerKeyF

type MapToInnerKeyF[V any, K any] func(V) K

type OnDeleteElemF

type OnDeleteElemF[K any, V any] func(k K, v V)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL