From 5fd644962bb287c05e05e85c2541d16fdbbca861 Mon Sep 17 00:00:00 2001 From: songzhibin97 <49082129+songzhibin97@users.noreply.github.com> Date: Fri, 2 Dec 2022 13:26:42 +0800 Subject: [PATCH] add: app readme --- app/bcache/README.md | 40 +++++++++++++++++++++++++++++++++ app/bcache/bcache.go | 2 ++ app/bcache/cache.go | 20 +++++++++++++++++ app/bconcurrent/README.md | 20 +++++++++++++++++ app/bconcurrent/fan_in.go | 2 +- app/bconcurrent/fan_in_test.go | 2 +- app/bconcurrent/fan_out.go | 2 +- app/bconcurrent/fan_out_test.go | 2 +- app/bconcurrent/map_reduce.go | 2 +- app/bconcurrent/or_done.go | 2 +- app/bconcurrent/or_done_test.go | 2 +- app/bconcurrent/orderly.go | 2 +- app/bconcurrent/orderly_test.go | 2 +- app/bconcurrent/pipeline.go | 2 +- app/bconcurrent/stream.go | 2 +- app/bconcurrent/stream_test.go | 2 +- 16 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 app/bcache/README.md create mode 100644 app/bcache/cache.go create mode 100644 app/bconcurrent/README.md diff --git a/app/bcache/README.md b/app/bcache/README.md new file mode 100644 index 0000000..81da9a8 --- /dev/null +++ b/app/bcache/README.md @@ -0,0 +1,40 @@ +# bcache + +缓存组件 + +## API +- Set 设置缓存,带失效时间 +- SetDefault 设置缓存,使用默认的缓存时间 +- SetNoExpire 设置缓存,不过期 +- SetIfAbsent 设置缓存,如果不存在设置成功返回bool +- Replace 替换缓存,如果存在设置成功返回bool +- Delete 删除缓存 +- Get 获取缓存,返回对应V以及bool +- GetWithExpire 获取缓存,返回对应V以及bool以及过期时间 +- Count 获取缓存数量 +- Clear 清空缓存 +- Load 从文件加载对象 +- Export 导出到文件 +- Marshal +- Unmarshal + +## EXAMPLE +```go +package main + +import ( + "fmt" + "github.com/songzhibin97/go-baseutils/app/bcache" + "github.com/songzhibin97/go-baseutils/base/bcomparator" + "time" +) + +func main() { + c := bcache.New[int, int](bcomparator.IntComparator()) + c.Set(1, 1, 5*time.Second) + fmt.Println(c.Get(1)) // 1,true + time.Sleep(5 * time.Second) + fmt.Println(c.Get(1)) // 0,false +} + +``` \ No newline at end of file diff --git a/app/bcache/bcache.go b/app/bcache/bcache.go index c19c8a6..e95ef10 100644 --- a/app/bcache/bcache.go +++ b/app/bcache/bcache.go @@ -17,6 +17,8 @@ const ( NoExpire time.Duration = -1 ) +var _ Cache[int, any] = (*BCache[int, any])(nil) + type BCache[K comparable, V any] struct { *bCache[K, V] } diff --git a/app/bcache/cache.go b/app/bcache/cache.go new file mode 100644 index 0000000..f5a5278 --- /dev/null +++ b/app/bcache/cache.go @@ -0,0 +1,20 @@ +package bcache + +import "time" + +type Cache[K comparable, V any] interface { + Set(k K, v V, d time.Duration) + SetDefault(k K, v V) + SetNoExpire(k K, v V) + SetIfAbsent(k K, v V, d time.Duration) bool + Replace(k K, v V, d time.Duration) bool + Delete(k K) + Get(k K) (V, bool) + GetWithExpire(k K) (V, time.Time, bool) + Count() int + Clear() + Load(data []byte) error + Export() ([]byte, error) + Marshal() ([]byte, error) + Unmarshal(data []byte) error +} diff --git a/app/bconcurrent/README.md b/app/bconcurrent/README.md new file mode 100644 index 0000000..44b266f --- /dev/null +++ b/app/bconcurrent/README.md @@ -0,0 +1,20 @@ +# bconcurrent + +channel一些最佳实践 + +## API +- FanInRec 扇入模式 +- MergeChannel 合并channel +- FanOut 扇出模式 +- MapChan 对channel中的元素进行map操作 +- ReduceChan 对channel中的元素进行reduce操作 +- OrDone 任意channel完成后返回 +- Orderly 顺序并发执行 +- Pipeline 串联执行 +- Stream 流式操作 +- TaskN 只取流中的前N个数据 +- TaskFn 筛选流中的数据,只保留满足条件的数据 +- TaskWhile 只取满足条件的数据,一旦不满足就不再取 +- SkipN 跳过流中的前N个数据 +- SkipFn 跳过满足条件的数据 +- SkipWhile 跳过满足条件的数据,一旦不满足,当前这个元素以后的元素都会输出 \ No newline at end of file diff --git a/app/bconcurrent/fan_in.go b/app/bconcurrent/fan_in.go index 98adf37..6c2ec56 100644 --- a/app/bconcurrent/fan_in.go +++ b/app/bconcurrent/fan_in.go @@ -1,4 +1,4 @@ -package concurrent +package bconcurrent import "reflect" diff --git a/app/bconcurrent/fan_in_test.go b/app/bconcurrent/fan_in_test.go index b30091e..e797047 100644 --- a/app/bconcurrent/fan_in_test.go +++ b/app/bconcurrent/fan_in_test.go @@ -1,4 +1,4 @@ -package concurrent +package bconcurrent import ( "sort" diff --git a/app/bconcurrent/fan_out.go b/app/bconcurrent/fan_out.go index c90188c..1172957 100644 --- a/app/bconcurrent/fan_out.go +++ b/app/bconcurrent/fan_out.go @@ -1,4 +1,4 @@ -package concurrent +package bconcurrent import "sync" diff --git a/app/bconcurrent/fan_out_test.go b/app/bconcurrent/fan_out_test.go index 97e7f37..b691cca 100644 --- a/app/bconcurrent/fan_out_test.go +++ b/app/bconcurrent/fan_out_test.go @@ -1,4 +1,4 @@ -package concurrent +package bconcurrent import ( "sort" diff --git a/app/bconcurrent/map_reduce.go b/app/bconcurrent/map_reduce.go index c939c46..88388cc 100644 --- a/app/bconcurrent/map_reduce.go +++ b/app/bconcurrent/map_reduce.go @@ -1,4 +1,4 @@ -package concurrent +package bconcurrent func MapChan[T any](in <-chan T, fn func(T) T) <-chan T { out := make(chan T, 1) diff --git a/app/bconcurrent/or_done.go b/app/bconcurrent/or_done.go index c87e180..8de0045 100644 --- a/app/bconcurrent/or_done.go +++ b/app/bconcurrent/or_done.go @@ -1,4 +1,4 @@ -package concurrent +package bconcurrent import "reflect" diff --git a/app/bconcurrent/or_done_test.go b/app/bconcurrent/or_done_test.go index 949fb79..d6b937e 100644 --- a/app/bconcurrent/or_done_test.go +++ b/app/bconcurrent/or_done_test.go @@ -1,4 +1,4 @@ -package concurrent +package bconcurrent import ( "testing" diff --git a/app/bconcurrent/orderly.go b/app/bconcurrent/orderly.go index b472fdb..6253506 100644 --- a/app/bconcurrent/orderly.go +++ b/app/bconcurrent/orderly.go @@ -1,4 +1,4 @@ -package concurrent +package bconcurrent import "sync" diff --git a/app/bconcurrent/orderly_test.go b/app/bconcurrent/orderly_test.go index d4f69eb..d8b0b27 100644 --- a/app/bconcurrent/orderly_test.go +++ b/app/bconcurrent/orderly_test.go @@ -1,4 +1,4 @@ -package concurrent +package bconcurrent import ( "fmt" diff --git a/app/bconcurrent/pipeline.go b/app/bconcurrent/pipeline.go index c5469f0..7bda7a0 100644 --- a/app/bconcurrent/pipeline.go +++ b/app/bconcurrent/pipeline.go @@ -1,4 +1,4 @@ -package concurrent +package bconcurrent // Pipeline 串联模式 func Pipeline[T any](in chan T) <-chan T { diff --git a/app/bconcurrent/stream.go b/app/bconcurrent/stream.go index d84a1ac..0d5bf1e 100644 --- a/app/bconcurrent/stream.go +++ b/app/bconcurrent/stream.go @@ -1,4 +1,4 @@ -package concurrent +package bconcurrent import "context" diff --git a/app/bconcurrent/stream_test.go b/app/bconcurrent/stream_test.go index a5f0042..e3a8604 100644 --- a/app/bconcurrent/stream_test.go +++ b/app/bconcurrent/stream_test.go @@ -1,4 +1,4 @@ -package concurrent +package bconcurrent import ( "context"