-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1451242
commit efa8b28
Showing
11 changed files
with
168 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package redistore | ||
|
||
import ( | ||
"github.com/dionysia-dev/dionysia/internal/config" | ||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
func NewClient(cfg *config.Config) *redis.Client { | ||
return redis.NewClient(&redis.Options{ | ||
Addr: cfg.RedisAddr, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package redistore | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/dionysia-dev/dionysia/internal/config" | ||
"github.com/dionysia-dev/dionysia/internal/service" | ||
"github.com/google/uuid" | ||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
type OriginStore struct { | ||
client *redis.Client | ||
ttl int | ||
} | ||
|
||
func NewOriginStore(client *redis.Client, cfg *config.Config) *OriginStore { | ||
return &OriginStore{ | ||
client: client, | ||
ttl: cfg.OriginTTL, | ||
} | ||
} | ||
|
||
func (s *OriginStore) Update(ctx context.Context, origin service.Origin) error { | ||
var updateOrigin = redis.NewScript(` | ||
local key = KEYS[1] | ||
local value = ARGV[1] | ||
local ttl = ARGV[2] | ||
local curr_value = redis.call("GET", key) | ||
if curr_value == value or curr_value == false then | ||
redis.call("SETEX", key, ttl, value) | ||
return 1 | ||
end | ||
return 0 | ||
`) | ||
|
||
_, err := updateOrigin.Run( | ||
ctx, | ||
s.client, | ||
[]string{origin.ID.String()}, origin.Address, s.ttl). | ||
Result() | ||
|
||
return err | ||
} | ||
|
||
func (s *OriginStore) Get(ctx context.Context, uuid uuid.UUID) (service.Origin, error) { | ||
address, err := s.client.Get(ctx, uuid.String()).Result() | ||
if err != nil { | ||
return service.Origin{}, err | ||
} | ||
|
||
return service.Origin{ | ||
ID: uuid, | ||
Address: address, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
type OriginStore interface { | ||
Update(context.Context, Origin) error | ||
Get(context.Context, uuid.UUID) (Origin, error) | ||
} | ||
|
||
type Origin struct { | ||
ID uuid.UUID | ||
Address string | ||
} | ||
|
||
type OriginHandler interface { | ||
Update(context.Context, Origin) error | ||
} | ||
|
||
type originHandler struct { | ||
store OriginStore | ||
} | ||
|
||
func NewOriginHandler(store OriginStore) OriginHandler { | ||
return &originHandler{ | ||
store: store, | ||
} | ||
} | ||
|
||
func (h *originHandler) Update(ctx context.Context, origin Origin) error { | ||
return h.store.Update(ctx, origin) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package service_test |