This commit is contained in:
56
item/expired.go
Normal file
56
item/expired.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package item
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/cache"
|
||||
)
|
||||
|
||||
//go:generate easyjson
|
||||
|
||||
//easyjson:json
|
||||
type expiredByte struct {
|
||||
Data []byte `json:"d"`
|
||||
Expired time.Time `json:"e"`
|
||||
}
|
||||
|
||||
func MarshalExpired(item *cache.Item) ([]byte, error) {
|
||||
var (
|
||||
e expiredByte
|
||||
err error
|
||||
)
|
||||
|
||||
e.Data, err = item.Marshal()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed marshal expired: %w", err)
|
||||
}
|
||||
|
||||
if item.TTL > 0 {
|
||||
e.Expired = item.Expired()
|
||||
}
|
||||
|
||||
return e.MarshalJSON()
|
||||
}
|
||||
|
||||
func UnmarshalExpired(item *cache.Item, d []byte) error {
|
||||
var e expiredByte
|
||||
|
||||
if err := e.UnmarshalJSON(d); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !e.Expired.IsZero() {
|
||||
item.TTL = time.Until(e.Expired)
|
||||
}
|
||||
|
||||
if item.IsExpired() {
|
||||
return cache.ErrCacheExpired
|
||||
}
|
||||
|
||||
if err := item.Unmarshal(e.Data); err != nil {
|
||||
return fmt.Errorf("failed unmarshal expired: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
99
item/expired_easyjson.go
Normal file
99
item/expired_easyjson.go
Normal file
@@ -0,0 +1,99 @@
|
||||
// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT.
|
||||
|
||||
package item
|
||||
|
||||
import (
|
||||
json "encoding/json"
|
||||
easyjson "github.com/mailru/easyjson"
|
||||
jlexer "github.com/mailru/easyjson/jlexer"
|
||||
jwriter "github.com/mailru/easyjson/jwriter"
|
||||
)
|
||||
|
||||
// suppress unused package warning
|
||||
var (
|
||||
_ *json.RawMessage
|
||||
_ *jlexer.Lexer
|
||||
_ *jwriter.Writer
|
||||
_ easyjson.Marshaler
|
||||
)
|
||||
|
||||
func easyjsonB8950805DecodeGitoaRuGo4devsCacheItem(in *jlexer.Lexer, out *expiredByte) {
|
||||
isTopLevel := in.IsStart()
|
||||
if in.IsNull() {
|
||||
if isTopLevel {
|
||||
in.Consumed()
|
||||
}
|
||||
in.Skip()
|
||||
return
|
||||
}
|
||||
in.Delim('{')
|
||||
for !in.IsDelim('}') {
|
||||
key := in.UnsafeFieldName(false)
|
||||
in.WantColon()
|
||||
if in.IsNull() {
|
||||
in.Skip()
|
||||
in.WantComma()
|
||||
continue
|
||||
}
|
||||
switch key {
|
||||
case "d":
|
||||
if in.IsNull() {
|
||||
in.Skip()
|
||||
out.Data = nil
|
||||
} else {
|
||||
out.Data = in.Bytes()
|
||||
}
|
||||
case "e":
|
||||
if data := in.Raw(); in.Ok() {
|
||||
in.AddError((out.Expired).UnmarshalJSON(data))
|
||||
}
|
||||
default:
|
||||
in.SkipRecursive()
|
||||
}
|
||||
in.WantComma()
|
||||
}
|
||||
in.Delim('}')
|
||||
if isTopLevel {
|
||||
in.Consumed()
|
||||
}
|
||||
}
|
||||
func easyjsonB8950805EncodeGitoaRuGo4devsCacheItem(out *jwriter.Writer, in expiredByte) {
|
||||
out.RawByte('{')
|
||||
first := true
|
||||
_ = first
|
||||
{
|
||||
const prefix string = ",\"d\":"
|
||||
out.RawString(prefix[1:])
|
||||
out.Base64Bytes(in.Data)
|
||||
}
|
||||
{
|
||||
const prefix string = ",\"e\":"
|
||||
out.RawString(prefix)
|
||||
out.Raw((in.Expired).MarshalJSON())
|
||||
}
|
||||
out.RawByte('}')
|
||||
}
|
||||
|
||||
// MarshalJSON supports json.Marshaler interface
|
||||
func (v expiredByte) MarshalJSON() ([]byte, error) {
|
||||
w := jwriter.Writer{}
|
||||
easyjsonB8950805EncodeGitoaRuGo4devsCacheItem(&w, v)
|
||||
return w.Buffer.BuildBytes(), w.Error
|
||||
}
|
||||
|
||||
// MarshalEasyJSON supports easyjson.Marshaler interface
|
||||
func (v expiredByte) MarshalEasyJSON(w *jwriter.Writer) {
|
||||
easyjsonB8950805EncodeGitoaRuGo4devsCacheItem(w, v)
|
||||
}
|
||||
|
||||
// UnmarshalJSON supports json.Unmarshaler interface
|
||||
func (v *expiredByte) UnmarshalJSON(data []byte) error {
|
||||
r := jlexer.Lexer{Data: data}
|
||||
easyjsonB8950805DecodeGitoaRuGo4devsCacheItem(&r, v)
|
||||
return r.Error()
|
||||
}
|
||||
|
||||
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
|
||||
func (v *expiredByte) UnmarshalEasyJSON(l *jlexer.Lexer) {
|
||||
easyjsonB8950805DecodeGitoaRuGo4devsCacheItem(l, v)
|
||||
}
|
||||
Reference in New Issue
Block a user