1"use strict";(self.webpackChunknonebot=self.webpackChunknonebot||[]).push([["4882"],{35973(e,n,i){i.r(n),i.d(n,{metadata:()=>l,default:()=>h,frontMatter:()=>s,contentTitle:()=>o,toc:()=>t,assets:()=>d});var l=JSON.parse('{"id":"best-practice/data-storing","title":"\u6570\u636E\u5B58\u50A8","description":"\u5B58\u50A8\u6570\u636E\u6587\u4EF6\u5230\u672C\u5730","source":"@site/versioned_docs/version-2.5.0/best-practice/data-storing.md","sourceDirName":"best-practice","slug":"/best-practice/data-storing","permalink":"/docs/best-practice/data-storing","draft":false,"unlisted":false,"editUrl":"https://github.com/nonebot/nonebot2/edit/master/website/versioned_docs/version-2.5.0/best-practice/data-storing.md","tags":[],"version":"2.5.0","lastUpdatedBy":"StarHeart","lastUpdatedAt":1785574664000,"sidebarPosition":1,"frontMatter":{"sidebar_position":1,"description":"\u5B58\u50A8\u6570\u636E\u6587\u4EF6\u5230\u672C\u5730"},"sidebar":"tutorial","previous":{"title":"\u5B9A\u65F6\u4EFB\u52A1","permalink":"/docs/best-practice/scheduler"},"next":{"title":"\u9519\u8BEF\u8DDF\u8E2A","permalink":"/docs/best-practice/error-tracking"}}'),r=i(62540),c=i(43023);let s={sidebar_position:1,description:"\u5B58\u50A8\u6570\u636E\u6587\u4EF6\u5230\u672C\u5730"},o="\u6570\u636E\u5B58\u50A8",d={},t=[{value:"\u5B89\u88C5\u63D2\u4EF6",id:"\u5B89\u88C5\u63D2\u4EF6",level:2},{value:"\u4F7F\u7528\u63D2\u4EF6",id:"\u4F7F\u7528\u63D2\u4EF6",level:2},{value:"\u914D\u7F6E\u9879",id:"\u914D\u7F6E\u9879",level:2},{value:"localstore_use_cwd",id:"localstore_use_cwd",level:3},{value:"localstore_cache_dir",id:"localstore_cache_dir",level:3},{value:"localstore_data_dir",id:"localstore_data_dir",level:3},{value:"localstore_config_dir",id:"localstore_config_dir",level:3},{value:"localstore_plugin_cache_dir",id:"localstore_plugin_cache_dir",level:3},{value:"localstore_plugin_data_dir",id:"localstore_plugin_data_dir",level:3},{value:"localstore_plugin_config_dir",id:"localstore_plugin_config_dir",level:3}];function a(e){let n={a:"a",admonition:"admonition",code:"code",h1:"h1",h2:"h2",h3:"h3",header:"header",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,c.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(n.header,{children:(0,r.jsx)(n.h1,{id:"\u6570\u636E\u5B58\u50A8",children:"\u6570\u636E\u5B58\u50A8"})}),"\n",(0,r.jsxs)(n.p,{children:["\u5728\u4F7F\u7528\u63D2\u4EF6\u7684\u8FC7\u7A0B\u4E2D\uFF0C\u96BE\u514D\u4F1A\u9700\u8981\u5B58\u50A8\u4E00\u4E9B\u6301\u4E45\u5316\u6570\u636E\uFF0C\u4F8B\u5982\u7528\u6237\u7684\u4E2A\u4EBA\u4FE1\u606F\u3001\u7FA4\u7EC4\u7684\u4FE1\u606F\u7B49\u3002\u9664\u4E86\u4F7F\u7528\u6570\u636E\u5E93\u7B49\u7B2C\u4E09\u65B9\u5B58\u50A8\u4E4B\u5916\uFF0C\u8FD8\u53EF\u4EE5\u4F7F\u7528\u672C\u5730\u6587\u4EF6\u6765\u81EA\u884C\u7BA1\u7406\u6570\u636E\u3002NoneBot \u63D0\u4F9B\u4E86 ",(0,r.jsx)(n.code,{children:"nonebot-plugin-localstore"})," \u63D2\u4EF6\uFF0C\u53EF\u7528\u4E8E\u83B7\u53D6\u6B63\u786E\u7684\u6570\u636E\u5B58\u50A8\u8DEF\u5F84\u5E76\u5199\u5165\u6570\u636E\u3002"]}),"\n",(0,r.jsx)(n.h2,{id:"\u5B89\u88C5\u63D2\u4EF6",children:"\u5B89\u88C5\u63D2\u4EF6"}),"\n",(0,r.jsxs)(n.p,{children:["\u5728\u4F7F\u7528\u524D\u8BF7\u5148\u5B89\u88C5 ",(0,r.jsx)(n.code,{children:"nonebot-plugin-localstore"})," \u63D2\u4EF6\u81F3\u9879\u76EE\u73AF\u5883\u4E2D\uFF0C\u53EF\u53C2\u8003",(0,r.jsx)(n.a,{href:"/docs/tutorial/store#%E5%AE%89%E8%A3%85%E6%8F%92%E4%BB%B6",children:"\u83B7\u53D6\u5546\u5E97\u63D2\u4EF6"}),"\u6765\u4E86\u89E3\u5E76\u9009\u62E9\u5B89\u88C5\u63D2\u4EF6\u7684\u65B9\u5F0F\u3002\u5982\uFF1A"]}),"\n",(0,r.jsxs)(n.p,{children:["\u5728",(0,r.jsx)(n.strong,{children:"\u9879\u76EE\u76EE\u5F55"}),"\u4E0B\u6267\u884C\u4EE5\u4E0B\u547D\u4EE4\uFF1A"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-bash",children:"nb plugin install nonebot-plugin-localstore\n"})}),"\n",(0,r.jsx)(n.h2,{id:"\u4F7F\u7528\u63D2\u4EF6",children:"\u4F7F\u7528\u63D2\u4EF6"}),"\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"nonebot-plugin-localstore"})," \u63D2\u4EF6\u517C\u5BB9 Windows\u3001Linux \u548C macOS \u7B49\u64CD\u4F5C\u7CFB\u7EDF\uFF0C\u4F7F\u7528\u65F6\u65E0\u9700\u5173\u5FC3\u64CD\u4F5C\u7CFB\u7EDF\u7684\u5DEE\u5F02\u3002\u540C\u65F6\u63D2\u4EF6\u63D0\u4F9B ",(0,r.jsx)(n.code,{children:"nb-cli"})," \u811A\u672C\uFF0C\u53EF\u4EE5\u4F7F\u7528 ",(0,r.jsx)(n.code,{children:"nb localstore"})," \u547D\u4EE4\u6765\u68C0\u67E5\u6570\u636E\u5B58\u50A8\u8DEF\u5F84\u3002"]}),"\n",(0,r.jsxs)(n.p,{children:["\u5728\u4F7F\u7528\u672C\u63D2\u4EF6\u524D\u540C\u6837\u9700\u8981\u4F7F\u7528 ",(0,r.jsx)(n.code,{children:"require"})," \u65B9\u6CD5\u8FDB\u884C",(0,r.jsx)(n.strong,{children:"\u52A0\u8F7D"}),"\u5E76",(0,r.jsx)(n.strong,{children:"\u5BFC\u5165"}),"\u9700\u8981\u4F7F\u7528\u7684\u65B9\u6CD5\uFF0C\u53EF\u53C2\u8003 ",(0,r.jsx)(n.a,{href:"/docs/advanced/requiring",children:"\u8DE8\u63D2\u4EF6\u8BBF\u95EE"})," \u4E00\u8282\u8FDB\u884C\u4E86\u89E3\uFF0C\u5982\uFF1A"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-python",children:'from nonebot import require\n\nrequire("nonebot_plugin_localstore")\n\nimport nonebot_plugin_localstore as store\n\n# \u83B7\u53D6\u63D2\u4EF6\u7F13\u5B58\u76EE\u5F55\ncache_dir = store.get_plugin_cache_dir()\n# \u83B7\u53D6\u63D2\u4EF6\u7F13\u5B58\u6587\u4EF6\ncache_file = store.get_plugin_cache_file("file_name")\n# \u83B7\u53D6\u63D2\u4EF6\u6570\u636E\u76EE\u5F55\ndata_dir = store.get_plugin_data_dir()\n# \u83B7\u53D6\u63D2\u4EF6\u6570\u636E\u6587\u4EF6\ndata_file = store.get_plugin_data_file("file_name")\n# \u83B7\u53D6\u63D2\u4EF6\u914D\u7F6E\u76EE\u5F55\nconfig_dir = store.get_plugin_config_dir()\n# \u83B7\u53D6\u63D2\u4EF6\u914D\u7F6E\u6587\u4EF6\nconfig_file = store.get_plugin_config_file("file_name")\n'})}),"\n",(0,r.jsx)(n.admonition,{title:"\u8B66\u544A",type:"danger",children:(0,r.jsx)(n.p,{children:"\u5728 Windows \u548C macOS \u7CFB\u7EDF\u4E0B\uFF0C\u63D2\u4EF6\u7684\u6570\u636E\u76EE\u5F55\u548C\u914D\u7F6E\u76EE\u5F55\u662F\u540C\u4E00\u4E2A\u76EE\u5F55\uFF0C\u56E0\u6B64\u5728\u4F7F\u7528\u65F6\u9700\u8981\u6CE8\u610F\u907F\u514D\u6587\u4EF6\u540D\u51B2\u7A81\u3002"})}),"\n",(0,r.jsxs)(n.p,{children:["\u63D2\u4EF6\u63D0\u4F9B\u7684\u65B9\u6CD5\u5747\u8FD4\u56DE\u4E00\u4E2A ",(0,r.jsx)(n.code,{children:"pathlib.Path"})," \u8DEF\u5F84\uFF0C\u53EF\u4EE5\u53C2\u8003 ",(0,r.jsx)(n.a,{href:"https://docs.python.org/zh-cn/3/library/pathlib.html",children:"pathlib \u6587\u6863"}),"\u6765\u4E86\u89E3\u5982\u4F55\u4F7F\u7528\u3002\u5E38\u7528\u7684\u65B9\u6CD5\u6709\uFF1A"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-python",children:'from pathlib import Path\n\ndata_file = store.get_plugin_data_file("file_name")\n# \u5199\u5165\u6587\u4EF6\u5185\u5BB9\ndata_file.write_text("Hello World!")\n# \u8BFB\u53D6\u6587\u4EF6\u5185\u5BB9\ndata = data_file.read_text()\n'})}),"\n",(0,r.jsx)(n.admonition,{title:"\u63D0\u793A",type:"note",children:(0,r.jsx)(n.p,{children:"\u5BF9\u4E8E\u5D4C\u5957\u63D2\u4EF6\uFF0C\u5B50\u63D2\u4EF6\u7684\u5B58\u50A8\u76EE\u5F55\u5C06\u4F4D\u4E8E\u7236\u63D2\u4EF6\u5B58\u50A8\u76EE\u5F55\u4E0B\u3002"})}),"\n",(0,r.jsx)(n.h2,{id:"\u914D\u7F6E\u9879",children:"\u914D\u7F6E\u9879"}),"\n",(0,r.jsx)(n.h3,{id:"localstore_use_cwd",children:"localstore_use_cwd"}),"\n",(0,r.jsx)(n.p,{children:"\u4F7F\u7528\u5F53\u524D\u5DE5\u4F5C\u76EE\u5F55\u4F5C\u4E3A\u6570\u636E\u5B58\u50A8\u76EE\u5F55\uFF0C\u4EE5\u4E0B\u6570\u636E\u76EE\u5F55\u914D\u7F6E\u9879\u9ED8\u8BA4\u503C\u5C06\u4F1A\u5BF9\u5E94\u53D8\u66F4"}),"\n",(0,r.jsxs)(n.p,{children:["\u9ED8\u8BA4\u503C\uFF1A",(0,r.jsx)(n.code,{children:"False"})]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-dotenv",children:"LOCALSTORE_USE_CWD=true\n"})}),"\n",(0,r.jsx)(n.h3,{id:"localstore_cache_dir",children:"localstore_cache_dir"}),"\n",(0,r.jsx)(n.p,{children:"\u81EA\u5B9A\u4E49\u7F13\u5B58\u76EE\u5F55"}),"\n",(0,r.jsx)(n.p,{children:"\u9ED8\u8BA4\u503C\uFF1A"}),"\n",(0,r.jsxs)(n.p,{children:["\u5F53 ",(0,r.jsx)(n.code,{children:"localstore_use_cwd"})," \u4E3A ",(0,r.jsx)(n.code,{children:"True"})," \u65F6\uFF0C\u7F13\u5B58\u76EE\u5F55\u4E3A ",(0,r.jsx)(n.code,{children:"<current_working_directory>/cache"}),"\uFF0C\u5426\u5219\uFF1A"]}),"\n",(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsxs)(n.li,{children:["macOS: ",(0,r.jsx)(n.code,{children:"~/Library/Caches/nonebot2"})]}),"\n",(0,r.jsxs)(n.li,{children:["Unix: ",(0,r.jsx)(n.code,{children:"~/.cache/nonebot2"})," (XDG default)"]}),"\n",(0,r.jsxs)(n.li,{children:["Windows: ",(0,r.jsx)(n.code,{children:"C:\\Users\\<username>\\AppData\\Local\\nonebot2\\Cache"})]}),"\n"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-dotenv",children:"LOCALSTORE_CACHE_DIR=/tmp/cache\n"})}),"\n",(0,r.jsx)(n.h3,{id:"localstore_data_dir",children:"localstore_data_dir"}),"\n",(0,r.jsx)(n.p,{children:"\u81EA\u5B9A\u4E49\u6570\u636E\u76EE\u5F55"}),"\n",(0,r.jsx)(n.p,{children:"\u9ED8\u8BA4\u503C\uFF1A"}),"\n",(0,r.jsxs)(n.p,{children:["\u5F53 ",(0,r.jsx)(n.code,{children:"localstore_use_cwd"})," \u4E3A ",(0,r.jsx)(n.code,{children:"True"})," \u65F6\uFF0C\u6570\u636E\u76EE\u5F55\u4E3A ",(0,r.jsx)(n.code,{children:"<current_working_directory>/data"}),"\uFF0C\u5426\u5219\uFF1A"]}),"\n",(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsxs)(n.li,{children:["macOS: ",(0,r.jsx)(n.code,{children:"~/Library/Application Support/nonebot2"})]}),"\n",(0,r.jsxs)(n.li,{children:["Unix: ",(0,r.jsx)(n.code,{children:"~/.local/share/nonebot2"})," or in $XDG_DATA_HOME, if defined"]}),"\n",(0,r.jsxs)(n.li,{children:["Win XP (not roaming): ",(0,r.jsx)(n.code,{children:"C:\\Documents and Settings\\<username>\\Application Data\\nonebot2"})]}),"\n",(0,r.jsxs)(n.li,{children:["Win 7 (not roaming): ",(0,r.jsx)(n.code,{children:"C:\\Users\\<username>\\AppData\\Local\\nonebot2"})]}),"\n"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-dotenv",children:"LOCALSTORE_DATA_DIR=/tmp/data\n"})}),"\n",(0,r.jsx)(n.h3,{id:"localstore_config_dir",children:"localstore_config_dir"}),"\n",(0,r.jsx)(n.p,{children:"\u81EA\u5B9A\u4E49\u914D\u7F6E\u76EE\u5F55"}),"\n",(0,r.jsx)(n.p,{children:"\u9ED8\u8BA4\u503C\uFF1A"}),"\n",(0,r.jsxs)(n.p,{children:["\u5F53 ",(0,r.jsx)(n.code,{children:"localstore_use_cwd"})," \u4E3A ",(0,r.jsx)(n.code,{children:"True"})," \u65F6\uFF0C\u914D\u7F6E\u76EE\u5F55\u4E3A ",(0,r.jsx)(n.code,{children:"<current_working_directory>/config"}),"\uFF0C\u5426\u5219\uFF1A"]}),"\n",(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsx)(n.li,{children:"macOS: same as user_data_dir"}),"\n",(0,r.jsxs)(n.li,{children:["Unix: ",(0,r.jsx)(n.code,{children:"~/.config/nonebot2"})]}),"\n",(0,r.jsxs)(n.li,{children:["Win XP (roaming): ",(0,r.jsx)(n.code,{children:"C:\\Documents and Settings\\<username>\\Local Settings\\Application Data\\nonebot2"})]}),"\n",(0,r.jsxs)(n.li,{children:["Win 7 (roaming): ",(0,r.jsx)(n.code,{children:"C:\\Users\\<username>\\AppData\\Roaming\\nonebot2"})]}),"\n"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-dotenv",children:"LOCALSTORE_CONFIG_DIR=/tmp/config\n"})}),"\n",(0,r.jsx)(n.h3,{id:"localstore_plugin_cache_dir",children:"localstore_plugin_cache_dir"}),"\n",(0,r.jsx)(n.p,{children:"\u81EA\u5B9A\u4E49\u63D2\u4EF6\u7F13\u5B58\u76EE\u5F55"}),"\n",(0,r.jsxs)(n.p,{children:["\u9ED8\u8BA4\u503C\uFF1A",(0,r.jsx)(n.code,{children:"{}"})]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-dotenv",children:'LOCALSTORE_PLUGIN_CACHE_DIR=\'\n{\n "plugin_id": "/tmp/plugin_cache"\n}\n\'\n'})}),"\n",(0,r.jsx)(n.h3,{id:"localstore_plugin_data_dir",children:"localstore_plugin_data_dir"}),"\n",(0,r.jsx)(n.p,{children:"\u81EA\u5B9A\u4E49\u63D2\u4EF6\u6570\u636E\u76EE\u5F55"}),"\n",(0,r.jsxs)(n.p,{children:["\u9ED8\u8BA4\u503C\uFF1A",(0,r.jsx)(n.code,{children:"{}"})]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-dotenv",children:'LOCALSTORE_PLUGIN_DATA_DIR=\'\n{\n "plugin_id": "/tmp/plugin_data"\n}\n\'\n'})}),"\n",(0,r.jsx)(n.h3,{id:"localstore_plugin_config_dir",children:"localstore_plugin_config_dir"}),"\n",(0,r.jsx)(n.p,{children:"\u81EA\u5B9A\u4E49\u63D2\u4EF6\u914D\u7F6E\u76EE\u5F55"}),"\n",(0,r.jsxs)(n.p,{children:["\u9ED8\u8BA4\u503C\uFF1A",(0,r.jsx)(n.code,{children:"{}"})]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-dotenv",children:'LOCALSTORE_PLUGIN_CONFIG_DIR=\'\n{\n "plugin_id": "/tmp/plugin_config"\n}\n\'\n'})})]})}function h(e={}){let{wrapper:n}={...(0,c.R)(),...e.components};return n?(0,r.jsx)(n,{...e,children:(0,r.jsx)(a,{...e})}):a(e)}},43023(e,n,i){i.d(n,{R:()=>s,x:()=>o});var l=i(63696);let r={},c=l.createContext(r);function s(e){let n=l.useContext(c);return l.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function o(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:s(e.components),l.createElement(c.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.