Update: session README
Showing
1 changed file
with
28 additions
and
25 deletions
| 1 | sessionmanager | 1 | session |
| 2 | ============== | 2 | ============== |
| 3 | 3 | ||
| 4 | sessionmanager is a golang session manager. It can use session for many providers.Just like the `database/sql` and `database/sql/driver`. | 4 | session is a Go session manager. It can use many session providers. Just like the `database/sql` and `database/sql/driver`. |
| 5 | 5 | ||
| 6 | ##How to install | 6 | ## How to install? |
| 7 | 7 | ||
| 8 | go get github.com/astaxie/beego/session | 8 | go get github.com/astaxie/beego/session |
| 9 | 9 | ||
| 10 | 10 | ||
| 11 | ##how many providers support | 11 | ## What providers are supported? |
| 12 | Now this sessionmanager support memory/file/redis/mysql | ||
| 13 | 12 | ||
| 13 | As of now this session manager support memory, file, Redis and MySQL. | ||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | ##How do we use it? | 16 | ## How to use it? |
| 17 | |||
| 18 | first you must import it | ||
| 19 | 17 | ||
| 18 | First you must import it | ||
| 20 | 19 | ||
| 21 | import ( | 20 | import ( |
| 22 | "github.com/astaxie/beego/session" | 21 | "github.com/astaxie/beego/session" |
| 23 | ) | 22 | ) |
| 24 | 23 | ||
| 25 | then in you web app init the globalsession manager | 24 | Then in you web app init the global session manager |
| 26 | 25 | ||
| 27 | var globalSessions *session.Manager | 26 | var globalSessions *session.Manager |
| 28 | 27 | ||
| 29 | use memory as providers: | 28 | * Use **memory** as provider: |
| 30 | 29 | ||
| 31 | func init() { | 30 | func init() { |
| 32 | globalSessions, _ = session.NewManager("memory", "gosessionid", 3600,"") | 31 | globalSessions, _ = session.NewManager("memory", "gosessionid", 3600,"") |
| 33 | go globalSessions.GC() | 32 | go globalSessions.GC() |
| 34 | } | 33 | } |
| 35 | 34 | ||
| 36 | use mysql as providers,the last param is the DNS, learn more from [mysql](https://github.com/Go-SQL-Driver/MySQL#dsn-data-source-name): | 35 | * Use **MySQL** as provider, the last param is the DNS, learn more from [mysql](https://github.com/Go-SQL-Driver/MySQL#dsn-data-source-name): |
| 37 | 36 | ||
| 38 | func init() { | 37 | func init() { |
| 39 | globalSessions, _ = session.NewManager("mysql", "gosessionid", 3600,"username:password@protocol(address)/dbname?param=value") | 38 | globalSessions, _ = session.NewManager( |
| 39 | "mysql", "gosessionid", 3600, "username:password@protocol(address)/dbname?param=value") | ||
| 40 | go globalSessions.GC() | 40 | go globalSessions.GC() |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | use file as providers,the last param is the path where to store the file: | 43 | * Use **file** as provider, the last param is the path where you want file to be stored: |
| 44 | 44 | ||
| 45 | func init() { | 45 | func init() { |
| 46 | globalSessions, _ = session.NewManager("file", "gosessionid", 3600,"./tmp") | 46 | globalSessions, _ = session.NewManager("file", "gosessionid", 3600, "./tmp") |
| 47 | go globalSessions.GC() | 47 | go globalSessions.GC() |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | use redis as providers,the last param is the redis's conn address: | 50 | * Use **Redis** as provider, the last param is the Redis conn address: |
| 51 | 51 | ||
| 52 | func init() { | 52 | func init() { |
| 53 | globalSessions, _ = session.NewManager("redis", "gosessionid", 3600,"127.0.0.1:6379") | 53 | globalSessions, _ = session.NewManager("redis", "gosessionid", 3600, "127.0.0.1:6379") |
| 54 | go globalSessions.GC() | 54 | go globalSessions.GC() |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | at last in the handlerfunc you can use it like this | 57 | Finally in the handlerfunc you can use it like this |
| 58 | 58 | ||
| 59 | func login(w http.ResponseWriter, r *http.Request) { | 59 | func login(w http.ResponseWriter, r *http.Request) { |
| 60 | sess := globalSessions.SessionStart(w, r) | 60 | sess := globalSessions.SessionStart(w, r) |
| ... | @@ -72,17 +72,19 @@ at last in the handlerfunc you can use it like this | ... | @@ -72,17 +72,19 @@ at last in the handlerfunc you can use it like this |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | 74 | ||
| 75 | ## How to write own provider? | ||
| 75 | 76 | ||
| 76 | ##How to write own provider | 77 | When you develop a web app, maybe you want to write own provider because you must meet the requirements. |
| 77 | When we develop a web app, maybe you want to write a provider because you must meet the requirements. | ||
| 78 | 78 | ||
| 79 | Write a provider is so easy. You only define two struct type(Session and Provider),which satisfy the interface definition.Maybe The memory provider is a good example for you. | 79 | Writing a provider is easy. You only need to define two struct types |
| 80 | (Session and Provider), which satisfy the interface definition. | ||
| 81 | Maybe you will find the **memory** provider as good example. | ||
| 80 | 82 | ||
| 81 | type SessionStore interface { | 83 | type SessionStore interface { |
| 82 | Set(key, value interface{}) error //set session value | 84 | Set(key, value interface{}) error // set session value |
| 83 | Get(key interface{}) interface{} //get session value | 85 | Get(key interface{}) interface{} // get session value |
| 84 | Delete(key interface{}) error //delete session value | 86 | Delete(key interface{}) error // delete session value |
| 85 | SessionID() string //back current sessionID | 87 | SessionID() string // return current sessionID |
| 86 | SessionRelease() // release the resource | 88 | SessionRelease() // release the resource |
| 87 | } | 89 | } |
| 88 | 90 | ||
| ... | @@ -93,6 +95,7 @@ Write a provider is so easy. You only define two struct type(Session and Provide | ... | @@ -93,6 +95,7 @@ Write a provider is so easy. You only define two struct type(Session and Provide |
| 93 | SessionGC() | 95 | SessionGC() |
| 94 | } | 96 | } |
| 95 | 97 | ||
| 96 | ##LICENSE | 98 | |
| 99 | ## LICENSE | ||
| 97 | 100 | ||
| 98 | BSD License http://creativecommons.org/licenses/BSD/ | 101 | BSD License http://creativecommons.org/licenses/BSD/ | ... | ... |
-
Please register or sign in to post a comment