Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some ideas about the framework #3

Closed
kcmvp opened this issue Jul 8, 2024 · 1 comment
Closed

Some ideas about the framework #3

kcmvp opened this issue Jul 8, 2024 · 1 comment

Comments

@kcmvp
Copy link

kcmvp commented Jul 8, 2024

cache := hot.NewHotCache[string, *User](hot.LRU, 100_000). WithLoaders(func(keys []string) (found map[string]*User, err error) { rows, err := db.Query("SELECT * FROM users WHERE id IN (?)", keys) // ... return users, err }). Build()

above code is from your example, from the point of engineering, may be we can leverage “inceptor” to make this automatically when cache miss the key instead of manually writing the sql. Due the fact of the limited of go reflection, may be we can use “coding by contractor”. it will use the parameter of the DAO method as the key. In fact that’s what spring framework of Java(via annotation) doing. Here is the contractor:

1: method must end with “Cachable”
2: the first parameter of the method should be a bool(saying cachable)

so the method is something like below

findUserByLastNameCachable(cachable bool, lastName string)

Of course this method is a method of DAO struct
Below is the implementation
1: define another method accept dao method as parameter
2: in the method it will first check the method naming standard to check it ends with “Cachable” or not
3: Check the passing method’s first parameter is a bool or not
4: if it’s bool and the value is true then it will query the cache, if it find the value the return. Otherwise it will go to database directly and save the result to the cache for later usage

@kcmvp kcmvp changed the title Some idea about the framework Some ideas about the framework Jul 8, 2024
@samber
Copy link
Owner

samber commented Sep 17, 2024

I'm not a big fan of reflection.

We can think about a higher-order abstraction, but using reflection is a no-go for me.

@samber samber closed this as completed Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants