ajout app
This commit is contained in:
64
Voltaserve/api/cache/file_cache.go
vendored
Normal file
64
Voltaserve/api/cache/file_cache.go
vendored
Normal file
@ -0,0 +1,64 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"voltaserve/infra"
|
||||
"voltaserve/model"
|
||||
"voltaserve/repo"
|
||||
)
|
||||
|
||||
type FileCache struct {
|
||||
redis *infra.RedisManager
|
||||
fileRepo repo.FileRepo
|
||||
keyPrefix string
|
||||
}
|
||||
|
||||
func NewFileCache() *FileCache {
|
||||
return &FileCache{
|
||||
fileRepo: repo.NewFileRepo(),
|
||||
redis: infra.NewRedisManager(),
|
||||
keyPrefix: "file:",
|
||||
}
|
||||
}
|
||||
|
||||
func (c *FileCache) Set(file model.File) error {
|
||||
b, err := json.Marshal(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = c.redis.Set(c.keyPrefix+file.GetID(), string(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *FileCache) Get(id string) (model.File, error) {
|
||||
value, err := c.redis.Get(c.keyPrefix + id)
|
||||
if err != nil {
|
||||
return c.Refresh(id)
|
||||
}
|
||||
file := repo.NewFile()
|
||||
if err = json.Unmarshal([]byte(value), &file); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (c *FileCache) Refresh(id string) (model.File, error) {
|
||||
res, err := c.fileRepo.Find(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = c.Set(res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *FileCache) Delete(id string) error {
|
||||
if err := c.redis.Delete(c.keyPrefix + id); err != nil {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
57
Voltaserve/api/cache/group_cache.go
vendored
Normal file
57
Voltaserve/api/cache/group_cache.go
vendored
Normal file
@ -0,0 +1,57 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"voltaserve/infra"
|
||||
"voltaserve/model"
|
||||
"voltaserve/repo"
|
||||
)
|
||||
|
||||
type GroupCache struct {
|
||||
redis *infra.RedisManager
|
||||
groupRepo repo.GroupRepo
|
||||
keyPrefix string
|
||||
}
|
||||
|
||||
func NewGroupCache() *GroupCache {
|
||||
return &GroupCache{
|
||||
redis: infra.NewRedisManager(),
|
||||
groupRepo: repo.NewGroupRepo(),
|
||||
keyPrefix: "group:",
|
||||
}
|
||||
}
|
||||
|
||||
func (c *GroupCache) Set(workspace model.Group) error {
|
||||
b, err := json.Marshal(workspace)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = c.redis.Set(c.keyPrefix+workspace.GetID(), string(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *GroupCache) Get(id string) (model.Group, error) {
|
||||
value, err := c.redis.Get(c.keyPrefix + id)
|
||||
if err != nil {
|
||||
return c.Refresh(id)
|
||||
}
|
||||
group := repo.NewGroup()
|
||||
if err = json.Unmarshal([]byte(value), &group); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return group, nil
|
||||
}
|
||||
|
||||
func (c *GroupCache) Refresh(id string) (model.Group, error) {
|
||||
res, err := c.groupRepo.Find(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = c.Set(res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
64
Voltaserve/api/cache/organization_cache.go
vendored
Normal file
64
Voltaserve/api/cache/organization_cache.go
vendored
Normal file
@ -0,0 +1,64 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"voltaserve/infra"
|
||||
"voltaserve/model"
|
||||
"voltaserve/repo"
|
||||
)
|
||||
|
||||
type OrganizationCache struct {
|
||||
redis *infra.RedisManager
|
||||
orgRepo repo.OrganizationRepo
|
||||
keyPrefix string
|
||||
}
|
||||
|
||||
func NewOrganizationCache() *OrganizationCache {
|
||||
return &OrganizationCache{
|
||||
redis: infra.NewRedisManager(),
|
||||
orgRepo: repo.NewOrganizationRepo(),
|
||||
keyPrefix: "organization:",
|
||||
}
|
||||
}
|
||||
|
||||
func (c *OrganizationCache) Set(organization model.Organization) error {
|
||||
b, err := json.Marshal(organization)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = c.redis.Set(c.keyPrefix+organization.GetID(), string(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *OrganizationCache) Get(id string) (model.Organization, error) {
|
||||
value, err := c.redis.Get(c.keyPrefix + id)
|
||||
if err != nil {
|
||||
return c.Refresh(id)
|
||||
}
|
||||
var org = repo.NewOrganization()
|
||||
if err = json.Unmarshal([]byte(value), &org); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return org, nil
|
||||
}
|
||||
|
||||
func (c *OrganizationCache) Refresh(id string) (model.Organization, error) {
|
||||
res, err := c.orgRepo.Find(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = c.Set(res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *OrganizationCache) Delete(id string) error {
|
||||
if err := c.redis.Delete(c.keyPrefix + id); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
64
Voltaserve/api/cache/workspace_cache.go
vendored
Normal file
64
Voltaserve/api/cache/workspace_cache.go
vendored
Normal file
@ -0,0 +1,64 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"voltaserve/infra"
|
||||
"voltaserve/model"
|
||||
"voltaserve/repo"
|
||||
)
|
||||
|
||||
type WorkspaceCache struct {
|
||||
redis *infra.RedisManager
|
||||
workspaceRepo repo.WorkspaceRepo
|
||||
keyPrefix string
|
||||
}
|
||||
|
||||
func NewWorkspaceCache() *WorkspaceCache {
|
||||
return &WorkspaceCache{
|
||||
redis: infra.NewRedisManager(),
|
||||
workspaceRepo: repo.NewWorkspaceRepo(),
|
||||
keyPrefix: "workspace:",
|
||||
}
|
||||
}
|
||||
|
||||
func (c *WorkspaceCache) Set(workspace model.Workspace) error {
|
||||
b, err := json.Marshal(workspace)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = c.redis.Set(c.keyPrefix+workspace.GetID(), string(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *WorkspaceCache) Get(id string) (model.Workspace, error) {
|
||||
value, err := c.redis.Get(c.keyPrefix + id)
|
||||
if err != nil {
|
||||
return c.Refresh(id)
|
||||
}
|
||||
workspace := repo.NewWorkspace()
|
||||
if err = json.Unmarshal([]byte(value), &workspace); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return workspace, nil
|
||||
}
|
||||
|
||||
func (c *WorkspaceCache) Refresh(id string) (model.Workspace, error) {
|
||||
res, err := c.workspaceRepo.Find(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = c.Set(res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *WorkspaceCache) Delete(id string) error {
|
||||
if err := c.redis.Delete(c.keyPrefix + id); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user