Skip to content

Commit

Permalink
Updates and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gaby authored Jan 7, 2025
1 parent 9dd5aa8 commit 79e9bfa
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
23 changes: 16 additions & 7 deletions objectbox/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

---
id: objectbox
title: ObjectBox
Expand All @@ -12,16 +11,16 @@ title: ObjectBox

An ObjectBox storage driver using [objectbox/objectbox-go](https://github.com/objectbox/objectbox-go).

**Note: Requires Go 1.19 and above**

### Table of Contents

- [Signatures](#signatures)
- [Installation](#installation)
- [Examples](#examples)
- [Config](#config)
- [Default Config](#default-config)

### Signatures

```go
func New(config ...Config) Storage
func (s *Storage) Get(key string) ([]byte, error)
Expand All @@ -32,26 +31,34 @@ func (s *Storage) Close() error
```

### Installation

[Install ojectbox](https://golang.objectbox.io/install)
```

```bash
bash <(curl -s https://raw.githubusercontent.com/objectbox/objectbox-go/main/install.sh)
```
Init your module
```sh

```bash
go mod init github.com/<user>/<repo>
```

And then install the objectbox implementation:
```sh

```bash
go get github.com/gofiber/storage/objectbox/v2
```

### Examples

Import the storage package.

```go
import "github.com/gofiber/storage/objectbox/v2"
```

You can use the following possibilities to create a storage:

```go
// Initialize default config
store := objectbox.New()
Expand All @@ -67,6 +74,7 @@ store := objectbox.New(objectbox.Config{
```

### Config

```go
type Config struct {
// Directory is the path where the database is stored.
Expand Down Expand Up @@ -97,6 +105,7 @@ type Config struct {
```

### Default Config

```go
var DefaultConfig = Config{
Directory: "objectbox_db",
Expand All @@ -105,4 +114,4 @@ var DefaultConfig = Config{
Reset: false,
CleanerInterval: 60 * time.Second,
}
```
```
3 changes: 1 addition & 2 deletions objectbox/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Config struct {

var DefaultConfig = Config{
Directory: "objectbox_db",
MaxSizeInKb: 1024 * 1024, // 1GByte
MaxSizeInKb: 1024 * 1024, // 1GB
MaxReaders: 126,
Reset: false,
CleanerInterval: 60 * time.Second,
Expand Down Expand Up @@ -59,5 +59,4 @@ func getConfig(config ...Config) Config {
}

return cfg

}
2 changes: 1 addition & 1 deletion objectbox/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gofiber/storage/objectbox/v2

go 1.23.3
go 1.23

require github.com/objectbox/objectbox-go v1.8.1

Expand Down
19 changes: 10 additions & 9 deletions objectbox/objectbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ func New(config ...Config) *Storage {

ob, err := objectbox.NewBuilder().Model(ObjectBoxModel()).MaxSizeInKb(cfg.MaxSizeInKb).MaxReaders(cfg.MaxReaders).Directory(cfg.Directory).Build()
if err != nil {
return nil
panic(err)
}

if cfg.Reset {
box := BoxForCache(ob)
box.RemoveAll()
err = box.RemoveAll()

if err != nil {
panic(err)
}
}

storage := &Storage{
Expand Down Expand Up @@ -62,7 +66,6 @@ func (s *Storage) Get(key string) ([]byte, error) {
}

return caches[0].Value, nil

}

// Set stores a value in cache with the specified key and expiration.
Expand Down Expand Up @@ -123,7 +126,6 @@ func (s *Storage) Delete(key string) error {
}

return nil

}

// Reset removes all entries from the cache.
Expand All @@ -139,10 +141,9 @@ func (s *Storage) Close() error {
return nil
}

// cleaneStorage removes all expired cache entries.
func (s *Storage) cleaneStorage() {
s.box.Query(Cache_.ExpiresAt.LessThan(time.Now().Unix())).Remove()

// cleanStorage removes all expired cache entries.
func (s *Storage) cleanStorage() {
s.box.Query(Cache_.ExpiresAt.LessThan(time.Now().Unix())).Remove() //nolint:errcheck // It is fine to ignore the error
}

// cleanerTicker runs periodic cleanup of expired entries.
Expand All @@ -153,7 +154,7 @@ func (s *Storage) cleanerTicker(interval time.Duration) {
for {
select {
case <-ticker.C:
s.cleaneStorage()
s.cleanStorage()
case <-s.done:
return
}
Expand Down
6 changes: 3 additions & 3 deletions objectbox/objectbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func Test_ObjectBox_Cleaner(t *testing.T) {
}

// Run cleanup
store.cleaneStorage()
store.cleanStorage()

// Verify expired items are removed and valid ones remain
for _, tt := range tests {
Expand Down Expand Up @@ -240,6 +240,7 @@ func Benchmark_ObjectBox_SetAndDelete(b *testing.B) {
b.ResetTimer()

var err error

for i := 0; i < b.N; i++ {
_ = store.Set("bench_key", []byte("bench_value"), 0)
err = store.Delete("bench_key")
Expand All @@ -248,7 +249,6 @@ func Benchmark_ObjectBox_SetAndDelete(b *testing.B) {
}

func Benchmark_ObjectBox_Cleaner(b *testing.B) {

for i := 0; i < 100; i++ {
key := fmt.Sprintf("expired-key-%d", i)
store.Set(key, []byte("benchmark-value"), -1*time.Second)
Expand All @@ -257,6 +257,6 @@ func Benchmark_ObjectBox_Cleaner(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
store.cleaneStorage()
store.cleanStorage()
}
}

0 comments on commit 79e9bfa

Please sign in to comment.