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) {
val, err := Client.Get("total-links").Result()
val, err := Client.Get(ctx, "total-links").Result()
if err != nil {
err := Client.Set("total-links", "0", 0).Err()
err := Client.Set(ctx, "total-links", "0", 0).Err()
if err != nil {
return 0, err
}
}
i, _ := strconv.Atoi(val)
i++
err = Client.Set("total-links", i, 0).Err()
err = Client.Set(ctx, "total-links", i, 0).Err()
if err != nil {
return 0, err
}

View file

@ -5,17 +5,17 @@ import (
)
func StatsIncreaseViewsLinks() (int, error) {
val, err := Client.Get("total-views").Result()
val, err := Client.Get(ctx, "total-views").Result()
if err != nil {
err := Client.Set("total-views", "0", 0).Err()
err := Client.Set(ctx, "total-views", "0", 0).Err()
if err != nil {
return 0, err
}
}
i, _ := strconv.Atoi(val)
i++
err = Client.Set("total-views", i, 0).Err()
err = Client.Set(ctx, "total-views", i, 0).Err()
if err != nil {
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 {
val1, err1 := Client.Get("total-links").Result()
val1, err1 := Client.Get(ctx, "total-links").Result()
if err1 != nil {
return 0
}
@ -17,7 +17,7 @@ func GetTotalLinks() int {
}
func GetTotalViews() int {
val1, err1 := Client.Get("total-views").Result()
val1, err1 := Client.Get(ctx, "total-views").Result()
if err1 != nil {
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
import (
"context"
"log"
"github.com/go-redis/redis"
"github.com/go-redis/redis/v8"
)
var ctx = context.Background()
var Client *redis.Client = redis.NewClient(&redis.Options{
Addr: "redis:6379",
Password: "",
DB: 0,
Addr: "redis:6379", // use default Addr
Password: "", // no password set
DB: 0, // use default DB
})
func Init_redis() {
_, err := Client.Ping().Result()
_, err := Client.Ping(ctx).Result()
if err != nil {
log.Fatal(err.Error())

View file

@ -9,7 +9,7 @@ import (
)
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 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 u.CLI {