car

package
v0.0.0-...-fae2274 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2018 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package car is a cache implementation using the cache replacement policy:

- Clock with adaptive replacement (CAR)

See https://www.cse.iitd.ernet.in/~sbansal/pubs/fast04.pdf for motivation, definition and contribution to all aspects of this package.

See https://en.wikipedia.org/wiki/Cache_replacement_policies for a general discussion of cache replacement policies.

Example
/*
   Golang CAR cache
   Copyright (C) 2018  Stefan Miller

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

package main

import (
	"fmt"

	"github.com/d024441/go-car/car"
)

var text = []string{
	"Clock with adaptive replacement (CAR)",
	"Golang",
	"Hello World",
	"Cache replacement policy",
}

func loadValue(key interface{}, slotNo int) interface{} {
	return text[key.(int)]
}

func replaceValue(key, value interface{}, slotNo int) {
	fmt.Printf("Key %d SlotNo %d\n", key, slotNo)
}

func main() {
	car := car.NewCAR(len(text) - 1)
	car.SetLoadValue(loadValue)
	car.SetReplaceValue(replaceValue)

	for k := range text {
		fmt.Println(car.Load(k))
	}

}
Output:

Clock with adaptive replacement (CAR)
Golang
Hello World
Key 0 SlotNo 0
Cache replacement policy

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CAR

type CAR struct {
	// contains filtered or unexported fields
}

A CAR is a cache with cache replacement policy: Clock with adaptive replacement (CAR).

func NewCAR

func NewCAR(n int) *CAR

NewCAR creates a CAR cache of n slots.

func (*CAR) Load

func (c *CAR) Load(key interface{}) interface{}

Load returns the cache value of the given cache key. Load is safe for concurrent use by multiple goroutines without additional locking or coordination.

func (*CAR) SetLoadValue

func (c *CAR) SetLoadValue(f LoadValue)

SetLoadValue sets the callback function for loading a value into the cache.

func (*CAR) SetReplaceValue

func (c *CAR) SetReplaceValue(f ReplaceValue)

SetReplaceValue sets the callback function for notifying cache replacements.

type LoadValue

type LoadValue func(key interface{}, slotNo int) interface{}

LoadValue is the type of a callback function to load values into the cache.

type ReplaceValue

type ReplaceValue func(key, value interface{}, slotNo int)

ReplaceValue is the type of a callback function to notify cache replacements.

Jump to

Keyboard shortcuts

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