Skip to content

Commit

Permalink
Introduce GobSink, a Sink using Gob encoding
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro Mery <[email protected]>
  • Loading branch information
amery committed Mar 13, 2023
1 parent 7e1cfa3 commit 30c612a
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 6 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ module github.com/darvaza-proxy/cache
go 1.19

require (
github.com/darvaza-proxy/core v0.5.3
github.com/darvaza-proxy/slog v0.4.5
github.com/mgechev/revive v1.2.5
)

require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/chavacava/garif v0.0.0-20230227094218-b8c73b2037b8 // indirect
github.com/fatih/color v1.14.1 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
Expand All @@ -21,5 +22,5 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/tools v0.6.0 // indirect
golang.org/x/tools v0.7.0 // indirect
)
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/chavacava/garif v0.0.0-20230227094218-b8c73b2037b8 h1:W9o46d2kbNL06lq7UNDPV0zYLzkrde/bjIqO02eoll0=
github.com/chavacava/garif v0.0.0-20230227094218-b8c73b2037b8/go.mod h1:gakxgyXaaPkxvLw1XQxNGK4I37ys9iBRzNUx/B7pUCo=
github.com/darvaza-proxy/core v0.5.3 h1:4UVliAeQUsW7KKVC1/zfRqzEONkqtpeodPa+iO3wid4=
github.com/darvaza-proxy/core v0.5.3/go.mod h1:ZImPFrOKEzwDTU5vS9dC7QpBAzl+3I8XhtnCkB6l5Ss=
github.com/darvaza-proxy/slog v0.4.5 h1:+tYMBCKGOqrAmQtR5wK+tU1+3WKIpx/h6B09qlph6IU=
github.com/darvaza-proxy/slog v0.4.5/go.mod h1:g+jWeOsFe+tKAKIgVBaVNmtECf15jYH1P9unVg+2gBI=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down Expand Up @@ -44,8 +46,8 @@ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
111 changes: 111 additions & 0 deletions gob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package cache

import (
"bytes"
"encoding/gob"
"time"

"github.com/darvaza-proxy/core"
)

var (
_ Sink = (*GobSink[any])(nil)
)

// GobSink is a Sink using generics for type safety and Gob
// for encoding
type GobSink[T any] struct {
out *T
e time.Time
bytes []byte
}

// Bytes returns the Gob encoded representation of the object
// in the Sink
func (sink *GobSink[T]) Bytes() []byte {
// TODO: do we need to copy them?
return sink.bytes
}

// Expire tells when this object will be evicted from the Cache
func (sink *GobSink[T]) Expire() time.Time {
return sink.e
}

// SetBytes sets the object of the GobSink and its expiration time
// from a Gob encoded byte array
func (sink *GobSink[T]) SetBytes(v []byte, e time.Time) error {
buf := bytes.NewBuffer(v)
dec := gob.NewDecoder(buf)

if err := dec.Decode(sink.out); err != nil {
// failed to decode
sink.Reset()
return err
}

// store
sink.bytes = make([]byte, len(v))
sink.e = e
copy(sink.bytes, v)
return nil
}

// SetString isn't supported by GobSink, but its needed to satisfy
// the Sink interface
func (sink *GobSink[T]) SetString(string, time.Time) error {
sink.Reset()
return ErrInvalid
}

// SetValue sets the object of the GobSink and its expiration time
func (sink *GobSink[T]) SetValue(v any, e time.Time) error {
var buf bytes.Buffer

p, ok := v.(*T)
if !ok {
return ErrInvalid
}

enc := gob.NewEncoder(&buf)
if err := enc.Encode(p); err != nil {
sink.Reset()
return err
}

sink.bytes = buf.Bytes()
sink.e = e
*sink.out = *p
return nil
}

// Value gives a copy of the stored object
func (sink *GobSink[T]) Value() T {
if sink.out == nil {
var zero T
return zero
}
return *sink.out
}

// Reset clears everything but the type pointer
// assigned during creation
func (sink *GobSink[T]) Reset() {
sink.bytes = []byte{}
sink.e = time.Time{}

if sink.out != nil {
var zero T
*sink.out = zero
}
}

// NewGobSink creates a new Sink using Gob as encoding
// and stores the object in the provided pointer
func NewGobSink[T any](out *T) *GobSink[T] {
if out == nil {
var zero T
core.Panicf("NewGobSink[%T]: output can not be nil", zero)
}
return &GobSink[T]{out: out}
}

0 comments on commit 30c612a

Please sign in to comment.