38 lines
751 B
Go
38 lines
751 B
Go
package cache
|
|
|
|
import (
|
|
"electricity_bill_calc/config"
|
|
"electricity_bill_calc/model"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
func SessionKey(keys ...string) string {
|
|
var b strings.Builder
|
|
b.WriteString(TAG_SESSION)
|
|
for _, s := range keys {
|
|
fmt.Fprintf(&b, ":%s", s)
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
func CacheSession(session *model.Session) error {
|
|
key := SessionKey(session.Token)
|
|
return Cache(key, session, config.ServiceSettings.MaxSessionLife)
|
|
}
|
|
|
|
func RetreiveSession(token string) (*model.Session, error) {
|
|
key := SessionKey(token)
|
|
return Retreive[model.Session](key)
|
|
}
|
|
|
|
func HasSession(token string) (bool, error) {
|
|
key := SessionKey(token)
|
|
return Exists(key)
|
|
}
|
|
|
|
func ClearSession(token string) (bool, error) {
|
|
key := SessionKey(token)
|
|
return Delete(key)
|
|
}
|