GithubHelp home page GithubHelp logo

zongfeizhang / lc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from simplejia/lc

0.0 0.0 0.0 9 KB

golang local cache with concurrency control, lock free【自带并发控制,服务降级的本地kv缓存】

License: BSD 2-Clause "Simplified" License

Go 100.00%

lc's Introduction

中文 README

lc (local cache)

Original Intention

  • Compared with lc, only using redis to cache is with these disadvantages such as network overhead, latency increasing dramatically if call repeatedly. In addition, using redis, when there is something wrong with the network, we cannot get data from redis. But in lc, even if the data is expired, we still can get the expired data.
  • When the cache is invalid, using mysql will take the risk of data penetration. While lc is with concurrency control which allows only one client of the same key at the same time to penetrate to the database. All other clients will return to the lc cache data.

Features

  • Local cache
  • Supporting the operation of Get,Set,Mget,Delete
  • When the cache is invalid, it not only returns the invalid flag, but also returns old data, for example: v, ok := lc.Get(key). When key expired and has not been deleted by lru, v is the stored value and ok returns false.
  • Lock free
  • Lru algorithm
  • Combining with lm, much simpler and quicker to use

demo

lc_test.go

package lc

import (
	"testing"
	"time"
)

func init() {
	Init(65536) // must init
}

func TestGetValid(t *testing.T) {
	key := "k"
	value := "v"
	Set(key, value, time.Second)
	time.Sleep(time.Millisecond * 10) // wait a while
	v, ok := Get(key)
	if !ok || v != value {
		t.Fatal("")
	}
}

中文

lc (local cache)

实现初衷

  • 纯用redis做缓存,相比lc,redis有网络调用开销,反复调用多次,延时急剧增大,当网络偶尔出现故障时,我们的数据接口也就拿不到数据,但lc里的数据就算是超过了设置的过期时间,我们一样能拿到过期的数据做备用
  • 使用mysql,当缓存失效,有数据穿透的风险,lc自带并发控制,有且只允许同一时间同一个key的唯一一个client穿透到数据库,其它直接返回lc缓存数据

特性

  • 本地缓存
  • 支持Get,Set,Mget,Delete操作
  • 当缓存失效时,返回失效标志同时,还返回旧的数据,如:v, ok := lc.Get(key),当key已经过了失效时间了,并且key还没有被lru淘汰掉,v是之前存的值,ok返回false
  • 实现代码没有用到锁
  • 使用到lru,淘汰长期不用的key
  • 结合lm使用更简单快捷

demo

一般使用方式:

vLc, ok := lc.Get(key)
v, _ := vLc.(int)
if ok {
   return
}

lc_test.go

package lc

import (
	"testing"
	"time"
)

func init() {
	Init(65536) // 使用lc之前必须要初始化
}

func TestGetValid(t *testing.T) {
	key := "k"
	value := "v"
	Set(key, value, time.Second)
	time.Sleep(time.Millisecond * 10) // 给异步处理留点时间
	v, ok := Get(key)
	if !ok || v != value {
		t.Fatal("")
	}
}

lc's People

Contributors

simplejia 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.