英文:
How to use Redis-based session on Revel
问题
如何在Revel上使用基于Redis的会话?
我找到了这个代码片段,但不知道如何使用它。
编辑 #1:
我的环境变量:
GOROOT --> /usr/lib/go
GOPATH --> /home/asd/Dropbox/go
我已经完成的步骤:
mkdir -p $GOPATH/src/myapp/app/libs/session
curl https://gist.githubusercontent.com/xthexder/9026678/raw/9e40fb56d8991de945a2064b6869bb7280b1305a/session.go \
> $GOPATH/src/myapp/app/libs/session/session.go
go get github.com/garyburd/redigo
go get github.com/robfig/revel
在init.go
中添加import "myapp/app/libs/session"
,然后出现了这个错误:
Go编译错误
Go代码Dropbox/go/src/myapp/app/libs/session/session.go无法编译:未定义:Redis
在Dropbox/go/src/myapp/app/libs/session/session.go(大约在第75行):
70: panic("Session values may not have null bytes")
71: }
72: sessionValue += "\x00" + key + ":" + value + "\x00"
73: }
74:
75: redisConn := Redis.Get()
76: defer redisConn.Close()
77:
78: params := []interface{}{"session:" + sessionToken, sessionValue}
79: if expireAfterDuration != 0 {
控制台上的错误信息:
WARN 2014/06/11 20:11:15 build.go:132: Cannot determine git repository version: exit status 128
ERROR 2014/06/11 20:11:15 build.go:84: # myapp/app/libs/session
Dropbox/go/src/myapp/app/libs/session/session.go:75: undefined: Redis
Dropbox/go/src/myapp/app/libs/session/session.go:126: undefined: Redis
Dropbox/go/src/myapp/app/libs/session/session.go:129: not enough arguments in call to redis.String
Dropbox/go/src/myapp/app/libs/session/session.go:172: undefined: Redis
Dropbox/go/src/myapp/app/libs/session/session.go:175: not enough arguments in call to redis.Int
英文:
How to use redis based-session on Revel?
I found this gist, but didn't know how to use it..
EDIT #1:
my env variable
GOROOT --> /usr/lib/go
GOPATH --> /home/asd/Dropbox/go
what i've done:
mkdir -p $GOPATH/src/myapp/app/libs/session
curl https://gist.githubusercontent.com/xthexder/9026678/raw/9e40fb56d8991de945a2064b6869bb7280b1305a/session.go \
> $GOPATH/src/myapp/app/libs/session/session.go
go get github.com/garyburd/redigo
go get github.com/robfig/revel
add import "myapp/app/libs/session"
on init.go
, and this error show up:
Go Compilation Error
The Go code Dropbox/go/src/myapp/app/libs/session/session.go does not compile: undefined: Redis
In Dropbox/go/src/myapp/app/libs/session/session.go (around line 75)
70: panic("Session values may not have null bytes")
71: }
72: sessionValue += "\x00" + key + ":" + value + "\x00"
73: }
74:
75: redisConn := Redis.Get()
76: defer redisConn.Close()
77:
78: params := []interface{}{"session:" + sessionToken, sessionValue}
79: if expireAfterDuration != 0 {
the error on the console:
WARN 2014/06/11 20:11:15 build.go:132: Cannot determine git repository version: exit status 128
ERROR 2014/06/11 20:11:15 build.go:84: # myapp/app/libs/session
Dropbox/go/src/myapp/app/libs/session/session.go:75: undefined: Redis
Dropbox/go/src/myapp/app/libs/session/session.go:126: undefined: Redis
Dropbox/go/src/myapp/app/libs/session/session.go:129: not enough arguments in call to redis.String
Dropbox/go/src/myapp/app/libs/session/session.go:172: undefined: Redis
Dropbox/go/src/myapp/app/libs/session/session.go:175: not enough arguments in call to redis.Int
答案1
得分: 1
看起来这个Gist已经过时了。
错误就在你的问题中。
Redis
没有定义。
首先,包名应该是小写的,这是一个明显的问题。从GitHub上看,你可以看到包名(现在)是小写的,正如它应该是的。
英文:
It looks like the Gist is out of date.
The error is right there in your question.
Redis
is not defined.
First of all the package name should be lower case, an immediate red flag. Looking at github you can seen the package is (now) lower case as it should be.
答案2
得分: 0
没问题,以下是翻译好的内容:
不用担心,我自己解决了,按照 http://www.diffchecker.com/n0k0fy8w 上显示的方式修改 session.go
代码。
将 init.go
中的代码从
revel.SessionFilter, // Restore and write the session cookie.
修改为
app.SessionFilter,
在控制器/动作中进行检查:
x, _ := strconv.ParseInt(c.Session["test"], 0, 64)
x += 1
c.Session["test"] = fmt.Sprintf("%d", x)
return c.Render(x)
在模板中:
{{.x}}
访问页面,并使用 redis-cli 进行验证:
$ redis-cli get `redis-cli --scan 'session:*'`
"\x00test:15\x00"
英文:
Nevermind, I solve it myself, modify the session.go
code as shown on http://www.diffchecker.com/n0k0fy8w
change init.go
from
revel.SessionFilter, // Restore and write the session cookie.
into
app.SessionFilter,
to check it on the controller/action:
x, _ := strconv.ParseInt(c.Session["test"], 0, 64)
x += 1
c.Session["test"] = fmt.Sprintf("%d", x)
return c.Render(x)
and on the template
{{.x}}
visit the page, and verify it using redis-cli
$ redis-cli get `redis-cli --scan 'session:*'`
"\x00test:15\x00"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论