Skip to content

Commit

Permalink
Refactor code to include cache functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Furkan-Gulsen committed Dec 13, 2023
1 parent 1d161fe commit 4d38928
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 32 deletions.
23 changes: 15 additions & 8 deletions domain/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,34 @@ import (
)

type Link struct {
db types.DB
db types.DB
cache types.Cache
}

func NewLinkDomain(d types.DB) *Link {
return &Link{db: d}
func NewLinkDomain(d types.DB, c types.Cache) *Link {
return &Link{db: d, cache: c}
}

func (service *Link) All(ctx context.Context) (*types.Link, error) {
func (service *Link) GetAllLinksFromDB(ctx context.Context) (*[]types.Link, error) {
links, err := service.db.All(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get all short URLs: %w", err)
}
return &links, nil
}

func (service *Link) Get(ctx context.Context, short string) (*types.Link, error) {
link, err := service.db.Get(ctx, short)
func (service *Link) GetOriginalURL(ctx context.Context, shortLinkKey string) (*string, error) {
link, err := service.cache.Get(ctx, shortLinkKey)
if err != nil {
return nil, fmt.Errorf("failed to get short URL for identifier '%s': %w", short, err)
return nil, fmt.Errorf("failed to get short URL for identifier '%s': %w", shortLinkKey, err)
}
return link, nil
return &link, nil
}

func (service *Link) Create(ctx context.Context, link types.Link) error {
if err := service.cache.Set(ctx, link.Id, link.OriginalURL); err != nil {
return fmt.Errorf("failed to set short URL for identifier '%s': %w", link.Id, err)
}
if err := service.db.Create(ctx, link); err != nil {
return fmt.Errorf("failed to create short URL: %w", err)
}
Expand All @@ -42,5 +46,8 @@ func (service *Link) Delete(ctx context.Context, short string) error {
if err := service.db.Delete(ctx, short); err != nil {
return fmt.Errorf("failed to delete short URL for identifier '%s': %w", short, err)
}
if err := service.cache.Delete(ctx, short); err != nil {
return fmt.Errorf("failed to delete short URL for identifier '%s': %w", short, err)
}
return nil
}
16 changes: 7 additions & 9 deletions functions/generate_link/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@ package main

import (
"context"
"fmt"
"os"

"github.com/Furkan-Gulsen/golang-url-shortener/config"
"github.com/Furkan-Gulsen/golang-url-shortener/domain"
"github.com/Furkan-Gulsen/golang-url-shortener/handlers"
"github.com/Furkan-Gulsen/golang-url-shortener/store"
"github.com/aws/aws-lambda-go/lambda"
)

func main() {
tableName, ok := os.LookupEnv("TABLE")
if !ok {
fmt.Println("Need TABLE environment variable")
tableName = "UrlShortenerTable"
}
appConfig := config.NewConfig()
redisAddress, redisPassword, redisDB := appConfig.GetRedisParams()
tableName := appConfig.GetTableName()

db := store.NewDynamoDBStore(context.TODO(), tableName)
// cache := store.NewRedisCache(context.TODO())
domain := domain.NewLinkDomain(db)
cache := store.NewRedisCache(redisAddress, redisPassword, redisDB)

domain := domain.NewLinkDomain(db, cache)
handler := handlers.NewAPIGatewayV2Handler(domain)
lambda.Start(handler.CreateShortLink)
}
4 changes: 3 additions & 1 deletion functions/generate_link/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/Furkan-Gulsen/golang-url-shortener/domain"
"github.com/Furkan-Gulsen/golang-url-shortener/handlers"
"github.com/Furkan-Gulsen/golang-url-shortener/mock"
"github.com/Furkan-Gulsen/golang-url-shortener/store"
"github.com/Furkan-Gulsen/golang-url-shortener/types"
"github.com/aws/aws-lambda-go/events"
"github.com/stretchr/testify/assert"
Expand All @@ -17,7 +18,8 @@ func setupTest(body string) (events.APIGatewayProxyResponse, error) {
mockStore := &mock.MockDynamoDBStore{
Links: map[string]types.Link{},
}
linkDomain := domain.NewLinkDomain(mockStore)
cache := store.NewRedisCache("localhost:6379", "", 0)
linkDomain := domain.NewLinkDomain(mockStore, cache)
apiHandler := handlers.NewAPIGatewayV2Handler(linkDomain)

request := events.APIGatewayV2HTTPRequest{Body: body}
Expand Down
15 changes: 7 additions & 8 deletions functions/redirect_link/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ package main

import (
"context"
"fmt"
"os"

"github.com/Furkan-Gulsen/golang-url-shortener/config"
"github.com/Furkan-Gulsen/golang-url-shortener/domain"
"github.com/Furkan-Gulsen/golang-url-shortener/handlers"
"github.com/Furkan-Gulsen/golang-url-shortener/store"
"github.com/aws/aws-lambda-go/lambda"
)

func main() {
tableName, ok := os.LookupEnv("TABLE")
if !ok {
fmt.Println("Need TABLE environment variable")
tableName = "UrlShortenerTable"
}
appConfig := config.NewConfig()
redisAddress, redisPassword, redisDB := appConfig.GetRedisParams()
tableName := appConfig.GetTableName()

db := store.NewDynamoDBStore(context.TODO(), tableName)
domain := domain.NewLinkDomain(db)
cache := store.NewRedisCache(redisAddress, redisPassword, redisDB)

domain := domain.NewLinkDomain(db, cache)
handler := handlers.NewAPIGatewayV2Handler(domain)
lambda.Start(handler.Redirect)
}
4 changes: 3 additions & 1 deletion functions/redirect_link/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
"github.com/Furkan-Gulsen/golang-url-shortener/domain"
"github.com/Furkan-Gulsen/golang-url-shortener/handlers"
"github.com/Furkan-Gulsen/golang-url-shortener/mock"
"github.com/Furkan-Gulsen/golang-url-shortener/store"
"github.com/aws/aws-lambda-go/events"
"github.com/stretchr/testify/assert"
)

func setupTest(shortLink string) (events.APIGatewayProxyResponse, error) {
mockStore := mock.NewMockDynamoDBStore()
linkDomain := domain.NewLinkDomain(mockStore)
cache := store.NewRedisCache("localhost:6379", "", 0)
linkDomain := domain.NewLinkDomain(mockStore, cache)
apiHandler := handlers.NewAPIGatewayV2Handler(linkDomain)

request := events.APIGatewayV2HTTPRequest{
Expand Down
10 changes: 5 additions & 5 deletions store/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func NewRedisCache(address string, password string, db int) *RedisCache {
return &RedisCache{client: client}
}

func (r *RedisCache) Set(ctx context.Context, key string, value interface{}) error {
return r.client.Set(ctx, key, value, time.Minute).Err()
func (r *RedisCache) Set(ctx context.Context, key string, val string) error {
return r.client.Set(ctx, key, val, time.Minute).Err()
}

func (r *RedisCache) Get(ctx context.Context, key string) (string, error) {
Expand All @@ -37,6 +37,6 @@ func (r *RedisCache) Delete(ctx context.Context, key string) error {
return r.client.Del(ctx, key).Err()
}

func (r *RedisCache) Close() error {
return r.client.Close()
}
// func (r *RedisCache) Close() error {
// return r.client.Close()
// }
10 changes: 10 additions & 0 deletions types/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package types

import "context"

type Cache interface {
Set(context.Context, string, string) error
Get(context.Context, string) (string, error)
Delete(context.Context, string) error
// Close() error
}

0 comments on commit 4d38928

Please sign in to comment.