You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
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
The text was updated successfully, but these errors were encountered:
kcmvp
changed the title
Some idea about the framework
Some ideas about the framework
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
The text was updated successfully, but these errors were encountered: