Skip to content

Commit

Permalink
feat(cache): add redis cache with lookup, persist and deactivate feature
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Oct 4, 2020
1 parent fc8c61b commit e85a297
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions cache/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package cache

import (
"log"
"net/http"
"os"
"sync"

"github.com/adhocore/urlsh/model"
"github.com/gomodule/redigo/redis"
)

var once sync.Once
var conn redis.Conn
var prefix = "url:"

func connect() redis.Conn {
cacheHost := os.Getenv("APP_CACHE_HOST")
if cacheHost == "" {
return nil
}

c, err := redis.Dial("tcp", cacheHost)
if err != nil {
log.Printf("%v", err)
return nil
}

return c
}

func Connection() redis.Conn {
once.Do(func() {
conn = connect()
})

return conn
}

func LookupURL(shortCode string) (model.URL, int) {
var urlModel model.URL
if nil == Connection() {
return urlModel, 0
}

line, err := conn.Do("GET", urlKey(model.URL{ShortCode: shortCode}))
if err != nil || line == nil {
return urlModel, 0
}

data := string(line.([]uint8))

// 0 = Inactive, 1 = Active
if data[0:1] == "0" {
return urlModel, http.StatusGone
}

urlModel.OriginURL = data[1:]
urlModel.ShortCode = shortCode

return urlModel, http.StatusFound
}

func DeactivateUrl(urlModel model.URL) {
cacheModel, status := LookupURL(urlModel.ShortCode)
if status == 0 {
return
}

urlModel.OriginURL = cacheModel.OriginURL
SavePopularUrl(urlModel, true)
}

func SavePopularUrl(urlModel model.URL, force bool) {
if nil == Connection() || (!force && hasUrl(urlModel)) {
return
}

_, _ = conn.Do("SET", urlKey(urlModel), urlValue(urlModel))
}

func hasUrl(urlModel model.URL) bool {
exist, err := conn.Do("EXISTS", urlKey(urlModel))

return err == nil && exist.(int64) > 0
}

func urlKey(urlModel model.URL) string {
return prefix + urlModel.ShortCode
}

func urlValue(urlModel model.URL) string {
active := "0"
if urlModel.IsActive() {
active = "1"
}

return active + urlModel.OriginURL
}

0 comments on commit e85a297

Please sign in to comment.