Changed Redis Layout for V8

This commit is contained in:
Phil 2022-01-13 20:06:30 +01:00
parent 9c14461b78
commit 82f79b733e
8 changed files with 30 additions and 15 deletions

View file

@ -5,17 +5,17 @@ import (
) )
func StatsIncreaseTotalLinks() (int, error) { func StatsIncreaseTotalLinks() (int, error) {
val, err := Client.Get("total-links").Result() val, err := Client.Get(ctx, "total-links").Result()
if err != nil { if err != nil {
err := Client.Set("total-links", "0", 0).Err() err := Client.Set(ctx, "total-links", "0", 0).Err()
if err != nil { if err != nil {
return 0, err return 0, err
} }
} }
i, _ := strconv.Atoi(val) i, _ := strconv.Atoi(val)
i++ i++
err = Client.Set("total-links", i, 0).Err() err = Client.Set(ctx, "total-links", i, 0).Err()
if err != nil { if err != nil {
return 0, err return 0, err
} }

View file

@ -5,17 +5,17 @@ import (
) )
func StatsIncreaseViewsLinks() (int, error) { func StatsIncreaseViewsLinks() (int, error) {
val, err := Client.Get("total-views").Result() val, err := Client.Get(ctx, "total-views").Result()
if err != nil { if err != nil {
err := Client.Set("total-views", "0", 0).Err() err := Client.Set(ctx, "total-views", "0", 0).Err()
if err != nil { if err != nil {
return 0, err return 0, err
} }
} }
i, _ := strconv.Atoi(val) i, _ := strconv.Atoi(val)
i++ i++
err = Client.Set("total-views", i, 0).Err() err = Client.Set(ctx, "total-views", i, 0).Err()
if err != nil { if err != nil {
return 0, err return 0, err
} }

5
db/Get.go Normal file
View file

@ -0,0 +1,5 @@
package db
func Get(input string) (string, error) {
return Client.Get(ctx, input).Result()
}

View file

@ -5,7 +5,7 @@ import (
) )
func GetTotalLinks() int { func GetTotalLinks() int {
val1, err1 := Client.Get("total-links").Result() val1, err1 := Client.Get(ctx, "total-links").Result()
if err1 != nil { if err1 != nil {
return 0 return 0
} }
@ -17,7 +17,7 @@ func GetTotalLinks() int {
} }
func GetTotalViews() int { func GetTotalViews() int {
val1, err1 := Client.Get("total-views").Result() val1, err1 := Client.Get(ctx, "total-views").Result()
if err1 != nil { if err1 != nil {
return 0 return 0
} }

7
db/Set.go Normal file
View file

@ -0,0 +1,7 @@
package db
import "time"
func Set(key string, value interface{}, time time.Duration) error {
return Client.Set(ctx, key, value, time).Err()
}

View file

@ -1,19 +1,22 @@
package db package db
import ( import (
"context"
"log" "log"
"github.com/go-redis/redis" "github.com/go-redis/redis/v8"
) )
var ctx = context.Background()
var Client *redis.Client = redis.NewClient(&redis.Options{ var Client *redis.Client = redis.NewClient(&redis.Options{
Addr: "redis:6379", Addr: "redis:6379", // use default Addr
Password: "", Password: "", // no password set
DB: 0, DB: 0, // use default DB
}) })
func Init_redis() { func Init_redis() {
_, err := Client.Ping().Result() _, err := Client.Ping(ctx).Result()
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())

View file

@ -9,7 +9,7 @@ import (
) )
func ID(c *fiber.Ctx) error { func ID(c *fiber.Ctx) error {
val, err := db.Client.Get(c.Params("id")).Result() val, err := db.Get(c.Params("id"))
if c.Get("CLI") == "1" { if c.Get("CLI") == "1" {
if err != nil { if err != nil {

View file

@ -83,7 +83,7 @@ func Posthome(c *fiber.Ctx) error {
}) })
} }
err = db.Client.Set(id, u.URL, 1296000*time.Second).Err() err = db.Set(id, u.URL, 1296000*time.Second)
if err != nil { if err != nil {
if u.CLI { if u.CLI {