{"id":201295,"date":"2025-05-10T09:27:30","date_gmt":"2025-05-10T01:27:30","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201295\/"},"modified":"2025-05-10T09:27:30","modified_gmt":"2025-05-10T01:27:30","slug":"redis%e7%bc%93%e5%ad%98%e5%ad%98%e5%82%a8session%e5%8e%9f%e7%90%86%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201295\/","title":{"rendered":"redis\u7f13\u5b58\u5b58\u50a8Session\u539f\u7406\u673a\u5236"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>redis\u7f13\u5b58\u5b58\u50a8Session\u539f\u7406\u673a\u5236<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u600e\u4e48\u5165\u95e8\u6570\u636e\u5e93\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762golang\u5b66\u4e60\u7f51\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aredis\u7f13\u5b58\u5b58\u50a8Session\u539f\u7406\u673a\u5236\u300b\uff0c\u6d89\u53ca\u5230\u5b58\u50a8\u3001Redissession\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<h2>\u57fa\u4e8e Redis \u5b58\u50a8 Session<\/h2>\n<p>\u5982\u679c\u6211\u4eec\u60f3\u5c06 session \u6570\u636e\u4fdd\u5b58\u5230 redis \u4e2d\uff0c\u53ea\u8981\u5c06 session \u7684\u5b58\u50a8\u5f15\u64ce\u6539\u6210 redis \u5373\u53ef\u3002<\/p>\n<p>\u4f7f\u7528 redis \u4f5c\u4e3a\u5b58\u50a8\u5f15\u64ce\u7684\u4f8b\u5b50\uff1a<\/p>\n<h3>\u9996\u5148\u5b89\u88c5 redis \u5b58\u50a8\u5f15\u64ce\u7684\u5305<\/h3>\n<pre>\ngo get github.com\/gin-contrib\/sessions\/redis\n<\/pre>\n<pre>\n\/\/ \u521d\u59cb\u5316\u57fa\u4e8e redis \u7684\u5b58\u50a8\u5f15\u64ce\n\/\/ \u53c2\u6570\u8bf4\u660e\uff1a \n\/\/ \u7b2c 1 \u4e2a\u53c2\u6570 - redis \u6700\u5927\u7684\u7a7a\u95f2\u8fde\u63a5\u6570 \n\/\/ \u7b2c 2 \u4e2a\u53c2\u6570 - \u6570\u901a\u4fe1\u534f\u8bae tcp \u6216\u8005 udp \n\/\/ \u7b2c 3 \u4e2a\u53c2\u6570 - redis \u5730\u5740, \u683c\u5f0f\uff0chost:port \n\/\/ \u7b2c 4 \u4e2a\u53c2\u6570 - redis \u5bc6\u7801\n\/\/ \u7b2c 5 \u4e2a\u53c2\u6570 - session \u52a0\u5bc6\u5bc6\u94a5\n\tstore, _ := redis.NewStore(10, \"tcp\", \"localhost:6379\", \"\", []byte(\"secret\"))\n\tr.Use(sessions.Sessions(\"mysession\", store))<\/pre>\n<h3>\u8bbe\u7f6esession\u8fc7\u671f\u65f6\u95f4<\/h3>\n<pre>\n\t\t\/\/\u914d\u7f6esession\u7684\u8fc7\u671f\u65f6\u95f4\n\t\tsession.Options(sessions.Options{MaxAge:3600*6 })\/\/6\u5c0f\u65f6=60*60*6<\/pre>\n<h3>\u5206\u5e03\u5f0f\u83b7\u53d6Session:(redis)<\/h3>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167257355463b17272a4807.png\" class=\"aligncenter\"><\/p>\n<pre>\n\u67e5\u770b\u5f53\u524d\u7684redis\u503c\uff1a\nkeys *\nset key vlaue \u8bbe\u7f6e\u952e\u503c\u5bf9\nget key \u67e5\u770b\u503c\uff08\u52a0\u5bc6\uff09<\/pre>\n<pre>\npackage main\nimport (\n\t\"github.com\/gin-contrib\/sessions\"\n\t\"github.com\/gin-contrib\/sessions\/cookie\"\n\t\"github.com\/gin-contrib\/sessions\/redis\"\n\t\"github.com\/gin-gonic\/gin\"\n)\nfunc main() {\n\tr := gin.Default() \n\t\/\/\u914d\u7f6esession\u7684\u4e2d\u95f4\u4ef6\n \tstore, _ := redis.NewStore(10, \"tcp\", \"localhost:6379\", \"\", []byte(\"secret\"))\n\tr.Use(sessions.Sessions(\"mysession\", store)) \n\t\/\/initMiddleware:\u914d\u7f6e\u8def\u7531\u4e2d\u95f4\u4ef6\n\tr.GET(\"\/\", func(c *gin.Context) {\n\t\t\/\/\u8bbe\u7f6esessions\n\t\tsession := sessions.Default(c)\n\t\t\/\/\u914d\u7f6esession\u7684\u8fc7\u671f\u65f6\u95f4\n\t\tsession.Options(sessions.Options{MaxAge:3600*6 })\/\/6\u5c0f\u65f6=60*60*6\n\t\tsession.Set(\"username\", \"\u6210\u5f3a\")\n\t\t\/\/\u4fdd\u5b58sessions:\u7ed9\u5176\u4ed6\u9875\u9762\u4f7f\u7528\uff08\u5fc5\u987b\u8c03\u7528\uff09\n\t\tsession.Save() \n\t\tc.String(200, \"gin\u9996\u9875\")\n\t})\n\tr.GET(\"\/news\", func(c *gin.Context) {\n\t\t\/\/\u83b7\u53d6sessions\n\t\tsession := sessions.Default(c)\n\t\tusername := session.Get(\"username\") \n\t\tc.String(200, \"username=%v\", username)\n\t})\n}<\/pre>\n<p style=\"text-align: left\">&nbsp;\u4ee5\u4e0a\u5c31\u662fredis\u7f13\u5b58\u5b58\u50a8Session\u539f\u7406\u673a\u5236\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8eredis\u5b58\u50a8Session\u7684\u8d44\u6599\u8bf7\u5173\u6ce8golang\u5b66\u4e60\u7f51\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>redis\u7f13\u5b58\u5b58\u50a8Session&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201295","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201295"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201295\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}