-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from tminaorg/cache
Cache
- Loading branch information
Showing
14 changed files
with
345 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ src/engines/*/site/* | |
|
||
logdump/*.html | ||
log/*.log | ||
database/ | ||
|
||
# go generate | ||
*_stringer.go | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package cache | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/tminaorg/brzaguza/src/bucket/result" | ||
) | ||
|
||
func Save(db DB, query string, results []result.Result) { | ||
log.Debug().Msg("Caching...") | ||
cacheTimer := time.Now() | ||
db.Set(query, results) | ||
log.Debug().Msgf("Cached results in %vns", time.Since(cacheTimer).Nanoseconds()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package cache | ||
|
||
type DB interface { | ||
Close() | ||
Set(k string, v Value) | ||
Get(k string, o Value) | ||
} | ||
|
||
type Value interface{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package pebble | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cockroachdb/pebble" | ||
"github.com/fxamacker/cbor/v2" | ||
"github.com/rs/zerolog/log" | ||
"github.com/tminaorg/brzaguza/src/cache" | ||
) | ||
|
||
type DB struct { | ||
pdb *pebble.DB | ||
} | ||
|
||
func New(path string) *DB { | ||
pdb, err := pebble.Open(fmt.Sprintf("%v/database", path), &pebble.Options{}) | ||
if err != nil { | ||
log.Panic().Msgf("Error opening pebble: %v (path: %v)", err, path) | ||
} else { | ||
log.Info().Msgf("Successfully opened pebble (path: %v)", path) | ||
} | ||
return &DB{pdb: pdb} | ||
} | ||
|
||
func (db *DB) Close() { | ||
if err := db.pdb.Close(); err != nil { | ||
log.Panic().Msgf("Error closing pebble: %v", err) | ||
} else { | ||
log.Debug().Msg("Successfully closed pebble") | ||
} | ||
} | ||
|
||
func (db *DB) Set(k string, v cache.Value) { | ||
if val, err := cbor.Marshal(v); err != nil { | ||
log.Error().Msgf("Error marshaling value: %v", err) | ||
} else if err := db.pdb.Set([]byte(k), val, pebble.Sync); err != nil { // should be set to NoSync when router gets graceful shutdown | ||
log.Panic().Msgf("Error setting KV to pebble: %v", err) | ||
} | ||
} | ||
|
||
func (db *DB) Get(k string, o cache.Value) { | ||
v, c, err := db.pdb.Get([]byte(k)) | ||
val := []byte(v) // copy data before closing, casting needed for unmarshal | ||
if err == pebble.ErrNotFound { | ||
log.Trace().Msgf("Found no value in pebble for key (%v): %v", k, err) | ||
} else if err != nil { | ||
log.Panic().Msgf("Error getting value from pebble for key (%v): %v", k, err) | ||
} else if err := c.Close(); err != nil { | ||
log.Panic().Msgf("Error closing io to pebble for key (%v): %v", k, err) | ||
} else if err := cbor.Unmarshal(val, o); err != nil { | ||
log.Error().Msgf("Failed unmarshaling value from pebble for key (%v): %v", k, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package redis | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/fxamacker/cbor/v2" | ||
"github.com/redis/go-redis/v9" | ||
"github.com/rs/zerolog/log" | ||
"github.com/tminaorg/brzaguza/src/cache" | ||
"github.com/tminaorg/brzaguza/src/config" | ||
) | ||
|
||
var ctx = context.Background() | ||
|
||
type DB struct { | ||
rdb *redis.Client | ||
} | ||
|
||
func New(config config.Redis) *DB { | ||
rdb := redis.NewClient(&redis.Options{ | ||
Addr: fmt.Sprintf("%v:%v", config.Host, config.Port), | ||
Password: config.Password, | ||
DB: int(config.Database), | ||
}) | ||
log.Info().Msgf("Successful connection to redis (addr: %v:%v/%v)", config.Host, config.Port, config.Database) | ||
return &DB{rdb: rdb} | ||
} | ||
|
||
// needed to comply with interface | ||
func (db *DB) Close() { | ||
log.Debug().Msg("Successfully disconnected from redis") | ||
} | ||
|
||
func (db *DB) Set(k string, v cache.Value) { | ||
if val, err := cbor.Marshal(v); err != nil { | ||
log.Error().Msgf("Error marshaling value: %v", err) | ||
} else if err := db.rdb.Set(ctx, k, val, 0).Err(); err != nil { | ||
log.Panic().Msgf("Error setting KV to redis: %v", err) | ||
} | ||
} | ||
|
||
func (db *DB) Get(k string, o cache.Value) { | ||
v, err := db.rdb.Get(ctx, k).Result() | ||
val := []byte(v) // copy data before closing, casting needed for unmarshal | ||
if err == redis.Nil { | ||
log.Trace().Msgf("Found no value in redis for key (%v): %v", k, err) | ||
} else if err != nil { | ||
log.Panic().Msgf("Error getting value from redis for key (%v): %v", k, err) | ||
} else if err := cbor.Unmarshal(val, o); err != nil { | ||
log.Error().Msgf("Failed unmarshaling value from redis for key (%v): %v", k, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,4 @@ type QwantResponse struct { | |
} `json:"items"` | ||
} `json:"result"` | ||
} `json:"data"` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.