一,安装用到的库
1,安装go-redis
liuhongdi@ku:~$ go get -u github.com/go-redis/redis
2,安装bigcache
liuhongdi@ku:~$ go get -u github.com/allegro/bigcache
说明:刘宏缔的go森林是一个专注golang的博客, 地址:https://blog.csdn.net/weixin_43881017
说明:作者:刘宏缔 邮箱: [email protected]
二,演示项目的相关信息
1,项目地址:
https://github.com/liuhongdi/digv10
2,项目功能:演示redis+bigcache两级缓存,
通过订阅redis消息更新进程内缓存bigcache
3,项目结构:如图:
三,go代码说明
1,config/config.yaml
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
2,controller/setController.go
-
-
-
-
"github.com/gin-gonic/gin"
-
"github.com/liuhongdi/digv10/global"
-
"github.com/liuhongdi/digv10/pkg/result"
-
-
-
type SetController struct{}
-
-
-
func NewSetController() SetController {
-
-
-
-
-
func (u *SetController) Pub(c *gin.Context) {
-
resultRes := result.NewResult(c)
-
-
-
err := global.RedisDb.Publish("articleMsg", data).Err()
-
-
resultRes.Error(400,err.Error())
-
-
-
resultRes.Success("发送成功")
-
-
3,bigcache/article.go
-
-
-
-
-
-
"github.com/liuhongdi/digv10/global"
-
"github.com/liuhongdi/digv10/model"
-
-
-
-
-
func getArticleCacheName(articleId uint64) (string) {
-
return "article_"+strconv.FormatUint(articleId,10)
-
-
-
-
func GetOneArticleBigCache(articleId uint64) (*model.Article,error) {
-
fmt.Println("bigcache:GetOneArticleBigCache")
-
key := getArticleCacheName(articleId);
-
val,err := global.BigCache.Get(key)
-
-
-
-
article := model.Article{}
-
if err := json.Unmarshal([]byte(val), &article); err != nil {
-
-
-
-
-
-
-
func SetOneArticleBigCache(articleId uint64,article *model.Article) (error) {
-
key := getArticleCacheName(articleId);
-
content,err := json.Marshal(article)
-
-
-
-
-
errSet := global.BigCache.Set(key,[]byte(content))
-
-
-
-
-
4,rediscache/article.go
-
-
-
-
-
-
"github.com/go-redis/redis"
-
"github.com/liuhongdi/digv10/global"
-
"github.com/liuhongdi/digv10/model"
-
-
-
-
-
const ArticleDuration = time.Minute * 10
-
-
-
func getArticleCacheName(articleId uint64) (string) {
-
return "article_"+strconv.FormatUint(articleId,10)
-
-
-
-
func GetOneArticleRedisCache(articleId uint64) (*model.Article,error) {
-
fmt.Println("redis:GetOneArticleRedisCache")
-
key := getArticleCacheName(articleId);
-
val, err := global.RedisDb.Get(key).Result()
-
-
if (err == redis.Nil || err != nil) {
-
-
-
article := model.Article{}
-
if err := json.Unmarshal([]byte(val), &article); err != nil {
-
-
-
-
-
-
-
-
func SetOneArticleRedisCache(articleId uint64,article *model.Article) (error) {
-
key := getArticleCacheName(articleId);
-
content,err := json.Marshal(article)
-
-
-
-
-
errSet := global.RedisDb.Set(key, content, ArticleDuration).Err()
-
-
-
-
-
5,service/article.go
-
-
-
-
-
"github.com/liuhongdi/digv10/bigcache"
-
"github.com/liuhongdi/digv10/dao"
-
"github.com/liuhongdi/digv10/global"
-
"github.com/liuhongdi/digv10/model"
-
"github.com/liuhongdi/digv10/rediscache"
-
-
-
-
-
func GetOneArticle(articleId uint64) (*model.Article, error) {
-
-
article,err := bigcache.GetOneArticleBigCache(articleId);
-
-
-
article,errSel := rediscache.GetOneArticleRedisCache(articleId)
-
-
-
article,errSel := dao.SelectOneArticle(articleId);
-
-
-
-
-
errSetR := rediscache.SetOneArticleRedisCache(articleId,article)
-
-
-
-
-
errSetB := bigcache.SetOneArticleBigCache(articleId,article)
-
-
-
-
-
-
-
-
-
errSet := bigcache.SetOneArticleBigCache(articleId,article)
-
-
-
-
-
-
-
-
-
-
-
-
-
func GetArticleSum() (int, error) {
-
return dao.SelectcountAll()
-
-
-
-
func GetArticleList(page int ,pageSize int) ([]*model.Article,error) {
-
articles, err := dao.SelectAllArticle(page,pageSize)
-
-
-
-
-
-
-
-
-
func UpdateArticleBigcache(articleId uint64) (error) {
-
-
article,errSel := rediscache.GetOneArticleRedisCache(articleId)
-
-
-
-
-
errSetB := bigcache.SetOneArticleBigCache(articleId, article)
-
-
-
-
-
-
-
-
-
-
func SubMessage(channel string) {
-
pubsub := global.RedisDb.Subscribe(channel)
-
fmt.Println("subscribe begin Receive")
-
_, err := pubsub.Receive()
-
-
-
-
fmt.Println("subscribe begin channel")
-
-
-
-
fmt.Println( msg.Channel, msg.Payload, "\r\n")
-
-
articleId,errUint := strconv.ParseUint(msg.Payload, 0, 64)
-
-
-
-
-
errB := UpdateArticleBigcache(articleId)
-
-
-
-
-
-
6,main.go
-
-
-
-
"github.com/gin-gonic/gin"
-
_ "github.com/jinzhu/gorm/dialects/mysql"
-
"github.com/liuhongdi/digv10/global"
-
"github.com/liuhongdi/digv10/router"
-
"github.com/liuhongdi/digv10/service"
-
-
-
-
-
-
-
err := global.SetupSetting()
-
-
log.Fatalf("init.setupSetting err: %v", err)
-
-
-
err = global.SetupDBLink()
-
-
log.Fatalf("init.setupDBEngine err: %v", err)
-
-
-
err = global.SetupRedisDb()
-
-
log.Fatalf("init.SetupRedisDb err: %v", err)
-
-
-
err = global.SetupBigCache()
-
-
log.Fatalf("init.SetupGlobalCache err: %v", err)
-
-
-
-
go service.SubMessage("articleMsg")
-
-
-
-
-
-
gin.SetMode(global.ServerSetting.RunMode)
-
-
-
-
r.Run(":"+global.ServerSetting.HttpPort)
-
7,global/bigcache.go
-
-
-
-
"github.com/allegro/bigcache"
-
-
-
-
-
-
BigCache *bigcache.BigCache
-
-
-
-
func SetupBigCache() (error) {
-
config := bigcache.Config {
-
-
LifeWindow: 3*time.Minute,
-
CleanWindow: 2*time.Minute,
-
-
-
-
-
-
BigCache, initErr = bigcache.NewBigCache(config)
-
-
-
-
-
-
-
8,global/redisDb.go
-
-
-
-
"github.com/go-redis/redis"
-
-
-
-
-
-
-
-
func SetupRedisDb() (error) {
-
-
RedisDb = redis.NewClient(&redis.Options{
-
-
Password: RedisSetting.Password,
-
-
-
-
_, err := RedisDb.Ping().Result()
-
-
-
-
-
9,global/setting.go
-
-
-
-
"github.com/liuhongdi/digv10/pkg/setting"
-
-
-
-
type ServerSettingS struct {
-
-
-
ReadTimeout time.Duration
-
WriteTimeout time.Duration
-
-
-
type DatabaseSettingS struct {
-
|
请发表评论