Skip to content

Commit

Permalink
Update table name and add Redis cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Furkan-Gulsen committed Dec 13, 2023
1 parent 489c882 commit 70fc20a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 8 deletions.
3 changes: 2 additions & 1 deletion functions/generate_link/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ func main() {
tableName, ok := os.LookupEnv("TABLE")
if !ok {
fmt.Println("Need TABLE environment variable")
tableName = "Links"
tableName = "UrlShortenerTable"
}

db := store.NewDynamoDBStore(context.TODO(), tableName)
// cache := store.NewRedisCache(context.TODO())
domain := domain.NewLinkDomain(db)
handler := handlers.NewAPIGatewayV2Handler(domain)
lambda.Start(handler.CreateShortLink)
Expand Down
2 changes: 1 addition & 1 deletion functions/redirect_link/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
tableName, ok := os.LookupEnv("TABLE")
if !ok {
fmt.Println("Need TABLE environment variable")
tableName = "Links"
tableName = "UrlShortenerTable"
}

db := store.NewDynamoDBStore(context.TODO(), tableName)
Expand Down
42 changes: 42 additions & 0 deletions store/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package store

import (
"context"
"time"

"github.com/go-redis/redis/v8"
)

type RedisCache struct {
client *redis.Client
}

func NewRedisCache(address string, password string, db int) *RedisCache {
client := redis.NewClient(&redis.Options{
Addr: address,
Password: password,
DB: db,
})

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) Get(ctx context.Context, key string) (string, error) {
val, err := r.client.Get(ctx, key).Result()
if err == redis.Nil {
return "", nil
}
return val, err
}

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()
}
56 changes: 50 additions & 6 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ Globals:
Tracing: Active
Environment:
Variables:
TABLE: !Ref Table
TABLE: !Ref UrlShortenerTable

Resources:
GenerateLink:
GenerateLinkFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/generate_link/
Expand All @@ -26,9 +26,9 @@ Resources:
Method: PUT
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref Table
TableName: !Ref UrlShortenerTable

RedirectLink:
RedirectLinkFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/redirect_link/
Expand All @@ -40,9 +40,9 @@ Resources:
Method: GET
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref Table
TableName: !Ref UrlShortenerTable

Table:
UrlShortenerTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
Expand Down Expand Up @@ -96,6 +96,46 @@ Resources:
HTTPSPort: 443
OriginProtocolPolicy: 'https-only'

UrlShortenerElastiCacheCluster:
Type: 'AWS::ElastiCache::CacheCluster'
Properties:
CacheNodeType: 'cache.t2.micro'
Engine: 'redis'
NumCacheNodes: 1
CacheSubnetGroupName: !Ref UrlShortenerCacheSubnetGroup
VpcSecurityGroupIds:
- !Ref UrlShortenerSecurityGroup

UrlShortenerCacheSubnetGroup:
Type: 'AWS::ElastiCache::SubnetGroup'
Properties:
Description: 'ElastiCache Subnet Group for URL Shortener'
SubnetIds:
- !Ref UrlShortenerSubnet

# Security Group for ElastiCache
UrlShortenerSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: 'ElastiCache Security Group for URL Shortener'
VpcId: !Ref UrlShortenerVPC

# VPC Configuration
UrlShortenerVPC:
Type: 'AWS::EC2::VPC'
Properties:
CidrBlock: '10.0.0.0/16'
EnableDnsSupport: true
EnableDnsHostnames: true

# Subnet Configuration
UrlShortenerSubnet:
Type: 'AWS::EC2::Subnet'
Properties:
VpcId: !Ref UrlShortenerVPC
CidrBlock: '10.0.1.0/24'
MapPublicIpOnLaunch: true

Outputs:
ApiUrl:
Description: 'API Gateway endpoint URL'
Expand All @@ -104,3 +144,7 @@ Outputs:
CloudFrontDistributionDomainName:
Description: 'CloudFront Distribution Domain Name'
Value: !GetAtt CloudFrontDistribution.DomainName

RedisEndpoint:
Description: 'Redis Cache Endpoint'
Value: !GetAtt MyRedisCache.RedisEndpoint.Address

0 comments on commit 70fc20a

Please sign in to comment.