Skip to content

Commit

Permalink
Merge pull request #8 from WqyJh/main
Browse files Browse the repository at this point in the history
Add batch processing
  • Loading branch information
yedf2 authored Feb 1, 2023
2 parents 2b5e6b5 + c49c67c commit e2bf8c1
Show file tree
Hide file tree
Showing 9 changed files with 927 additions and 10 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The first Redis cache library to ensure eventual consistency and strong consiste
- Anti-breakdown: a better solution for cache breakdown
- Anti-penetration
- Anti-avalanche
- Batch Query

## Usage
This cache repository uses the most common `update DB and then delete cache` cache management policy
Expand All @@ -41,6 +42,38 @@ v, err := rc.Fetch("key1", 300 * time.Second, func()(string, error) {
rc.TagAsDeleted(key)
```

## Batch usage

### Batch read cache
``` Go
import "github.com/dtm-labs/rockscache"

// new a client for rockscache using the default options
rc := rockscache.NewClient(redisClient, NewDefaultOptions())

// use FetchBatch to fetch data
// 1. the first parameter is the keys list of the data
// 2. the second parameter is the data expiration time
// 3. the third parameter is the batch data fetch function which is called when the cache does not exist
// the parameter of the batch data fetch function is the index list of those keys
// missing in cache, which can be used to form a batch query for missing data.
// the return value of the batch data fetch function is a map, with key of the
// index and value of the corresponding data in form of string
v, err := rc.FetchBatch([]string{"key1", "key2", "key3"}, 300, func(idxs []int) (map[int]string, error) {
// fetch data from database or other sources
values := make(map[int]string)
for _, i := range idxs {
values[i] = fmt.Sprintf("value%d", i)
}
return values, nil
})
```

### Batch delete cache
``` Go
rc.TagAsDeletedBatch(keys)
```

## Eventual consistency
With the introduction of caching, consistency problems in a distributed system show up, as the data is stored in two places at the same time: the database and Redis. For background on this consistency problem, and an introduction to popular Redis caching solutions, see.
- [https://yunpengn.github.io/blog/2019/05/04/consistent-redis-sql/](https://yunpengn.github.io/blog/2019/05/04/consistent-redis-sql/)
Expand Down
Loading

0 comments on commit e2bf8c1

Please sign in to comment.