-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb3344d
commit d822a79
Showing
9 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[redis] | ||
ip=127.0.0.1 | ||
port=6379 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
package controller | ||
|
||
import "github.com/gin-gonic/gin" | ||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/taoshihan1991/miaosha/redis" | ||
) | ||
|
||
func GetProduct(c *gin.Context) { | ||
id := c.Query("id") | ||
redis.NewRedis() | ||
c.JSON(200, gin.H{ | ||
"code": 200, | ||
"msg": "success", | ||
"data": nil, | ||
"data": redis.ProductInfo(id), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package redis | ||
|
||
func ProductInfo(id string) map[string]string { | ||
key := "product:" + id | ||
return HashGet(key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package redis | ||
|
||
import ( | ||
"context" | ||
"github.com/go-redis/redis/v8" | ||
"github.com/taoshihan1991/miaosha/setting" | ||
"log" | ||
) | ||
|
||
var rdb *redis.Client | ||
var ctx = context.Background() | ||
|
||
func NewRedis() { | ||
rdb = redis.NewClient(&redis.Options{ | ||
Addr: setting.Redis.Ip + ":" + setting.Redis.Port, | ||
Password: "", // no password set | ||
DB: 0, // use default DB | ||
}) | ||
} | ||
func GetStr(key string) string { | ||
str, err := rdb.Get(ctx, key).Result() | ||
if err != nil { | ||
log.Println(err.Error()) | ||
return "" | ||
} | ||
return str | ||
} | ||
func HashGet(key string) map[string]string { | ||
res, err := rdb.HGetAll(ctx, key).Result() | ||
if err != nil { | ||
log.Println(err.Error()) | ||
return make(map[string]string) | ||
} | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package setting | ||
|
||
import ( | ||
"log" | ||
) | ||
import "github.com/Unknwon/goconfig" | ||
|
||
var Config *GlobalConfig | ||
var Redis *RedisConfig | ||
var configfile *goconfig.ConfigFile | ||
var err error | ||
|
||
func GetConfigIni(filepath string) error { | ||
|
||
configfile, err = goconfig.LoadConfigFile(filepath) | ||
if err != nil { | ||
log.Println("配置文件读取错误,找不到配置文件", err.Error()) | ||
return err | ||
} | ||
|
||
Config = &GlobalConfig{} | ||
return nil | ||
} | ||
func GetRedisConfig() error { | ||
Redis = &RedisConfig{} | ||
r, err := configfile.GetSection("redis") | ||
if err != nil { | ||
return err | ||
} | ||
for key, val := range r { | ||
if key == "ip" { | ||
Redis.Ip = val | ||
} | ||
if key == "port" { | ||
Redis.Port = val | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package setting | ||
|
||
type GlobalConfig struct { | ||
Redis RedisConfig `json:"redis"` | ||
Database DatabaseConfig `json:"database"` | ||
} | ||
type RedisConfig struct { | ||
Ip string | ||
Port string | ||
} | ||
type DatabaseConfig struct { | ||
} |