Coder Social home page Coder Social logo

cch-rs's Introduction

cch.rs

Simple Cache provider

usage

  1. add dependency into Cargo.toml
[dependencies]
cch = { git = "https://github.com/0x75960/cch-rs", branch = "master" }
  1. import and use in your code
extern crate cch;

use cch::*;

struct TestCacheDriver {
	seen: Vec<i32>,
}

impl CacheDriver for TestCacheDriver {
	type Item = i32;
	type Key = i32;

	fn add(&mut self, item: Self::Item) {
		self.seen.push(item);
	}

	fn has_item(&mut self, key: Self::Key) -> bool {
		for i in &self.seen {
			if *i == key {
				return true;
			}
		}
		false
	}

	fn remove(&mut self, _key: Self::Key) {
		self.seen.pop();
	}

	fn dump(&mut self) {
		assert!(self.seen.len() == 6);
	}

	fn load(&mut self) {
		assert!(self.seen.len() == 0);
		for i in vec![1, 2, 3] {
			self.seen.push(i);
		}
	}
}

fn main() {
	let driver = TestCacheDriver{
		seen: vec![]
	};

	let mut cache = Cache::with(driver);

	for i in vec![1, 2, 3] {
		// check initial cache
		assert_eq!(cache.has_item(i), true);
	}

	for i in vec![4, 5, 6] {
		// check not cached yet
		assert_eq!(cache.has_item(i), false);
		// and cache item
		cache.add(i);
	}

	for i in vec![4, 5, 6] {
		// check cached
		assert_eq!(cache.has_item(i), true);
	}

	cache.add(1);
	cache.remove(1);
}

test

cargo test

cch-rs's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.