Comments (6)
Thank you. Now I understand. We use string as a key just for as it was our historical use case. Now, Go supports generics so maybe we can make it more liberal in what we accept (e.g. accept comparable
and also generify the hasher interface).
What do you think? This change will be then backward compatible (except requirement for newer go version).
from bigcache.
Hey, could post a sample code. I don't get the question?
from bigcache.
@janisz Sure thing.
We're already hashing obj
in different places, and use the same hashing approach for other use-cases as well. So I'm opting to format/parse instead of add a new unexpected hasher. But would be nice to just reuse the uint64
I already have.
package main
import (
"context"
"fmt"
"strconv"
"time"
"github.com/allegro/bigcache/v3"
pb "github.com/myco/protos/foobarbaz"
)
func main() {
cache := NewBigCache()
obj := getObject()
key := hashObject(obj)
// Because cache.Set() takes a string, now I have to convert
cache.Set(strconv.FormatUint(key, 10), obj)
}
func hashObject(obj *pb.Object) uint64 {
// Internal xxh3 sync.Pool for buffer reuse
hasher := Hasher()
defer ReleaseHasher(hasher)
hashString(hasher, obj.Foo)
hashBar(hasher, obj.Bar)
return hasher.Sum64()
}
func hashBar(hasher Hasher, bar *pb.Bar) {
// Long switch-statement to deal with one-of values
}
func NewBigCache() *bigcache.BigCache {
cache, err := bigcache.New(context.TODO(), bigcache.Config{
Shards: 1024,
HardMaxCacheSize: 1024,
LifeWindow: time.Minute * 5,
CleanWindow: time.Second * 150,
MaxEntriesInWindow: 150000,
MaxEntrySize: 2048,
// Relevant bit
Hasher: dumbHasher{},
})
if err != nil {
panic(err)
}
return cache
}
type dumbHasher struct{}
// Sum64 assumes that 's' is a string-formatted uint64 and panics if not
func (p dumbHasher) Sum64(s string) uint64 {
u, err := strconv.ParseUint(s, 10, 64)
if err != nil {
panic(fmt.Sprintf("invalid argument: cannot convert %q to a uint64", s))
}
return u
}
from bigcache.
We use string as a key just for as it was our historical use case
But it's uint64
internally isn't it? Or was that a change at some point.
Either way, @janisz, it sounds great to me. :)
from bigcache.
But it's uint64 internally isn't it? Or was that a change at some point.
Correct and we should keep it that way. So the only change will be in public methods and hasher. So hasher need to convert T comparable
into uint64
I'm not sure if we then can provide defulat hasher easliy. Maybe we need to cut v4 and have BigCache
and GenericBigCache
from bigcache.
Or have a different package so that the name isn't compromised. We did this with lazylru
:
github.com/TriggerMail/lazylru/generic.LazyLRU
github.com/TriggerMail/lazylru.LazyLRU
from bigcache.
Related Issues (20)
- LifeWindow Not valid HOT 1
- Error logs about bytes allocation? HOT 1
- why did my key expired HOT 7
- resetKeyFromEntry HOT 1
- Excessive memory allocation HOT 1
- The index(int) of entries(queue.BytesQueue) overflows the hashmap's uint32 value HOT 4
- Memory so high, and when clean not reduce size HOT 5
- Benchmark is not fair, So the speed can't be trusted HOT 4
- Whether it is a bug ? HOT 1
- Why doesn't bigcache consider designing expiration time for each cache key? HOT 4
- How does bigcache handle hash collisions HOT 1
- BigCache is logging allocations it should not HOT 4
- Support for the map? HOT 1
- compile a 32-bit LSB executable on arm and Execution error-> panic: unaligned 64-bit atomic operation HOT 4
- Is there a memory leak in bigcache? HOT 4
- Add multi-level cache design HOT 2
- Why do we need to configure both lifeWindow and cleanWindow? HOT 2
- GRPC server support HOT 4
- Proposal to Integrate SIEVE Eviction Algorithm
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bigcache.