"use strict";(self.webpackChunkgoffee_site=self.webpackChunkgoffee_site||[]).push([[5075],{7268:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>s,contentTitle:()=>i,default:()=>d,frontMatter:()=>a,metadata:()=>r,toc:()=>h});var c=t(4848),o=t(8453);const a={title:"Cache"},i=void 0,r={id:"cache",title:"Cache",description:"Goffee uses Redis for cache and its disabled by default, you can enable it in the file config/cache.go by setting the attribute EnableCache to true, then add Redis connection information to the .env if you are using it. otherwise, you can use an external tool to inject these variables into the environment",source:"@site/docs/cache.md",sourceDirName:".",slug:"/cache",permalink:"/goffee/docs/cache",draft:!1,unlisted:!1,tags:[],version:"current",frontMatter:{title:"Cache"},sidebar:"docsSidebar",previous:{title:"Authority",permalink:"/goffee/docs/authority"},next:{title:"TLS",permalink:"/goffee/docs/tls"}},s={},h=[{value:"Set values in the cache",id:"set-values-in-the-cache",level:4},{value:"Set values in the cache with an expiration date",id:"set-values-in-the-cache-with-an-expiration-date",level:4},{value:"Get values from cache",id:"get-values-from-cache",level:4},{value:"Cache Delete",id:"cache-delete",level:4}];function l(e){const n={code:"code",h4:"h4",p:"p",pre:"pre",...(0,o.R)(),...e.components};return(0,c.jsxs)(c.Fragment,{children:[(0,c.jsxs)(n.p,{children:["Goffee uses ",(0,c.jsx)(n.code,{children:"Redis"})," for cache and its disabled by default, you can enable it in the file ",(0,c.jsx)(n.code,{children:"config/cache.go"})," by setting the attribute ",(0,c.jsx)(n.code,{children:"EnableCache"})," to ",(0,c.jsx)(n.code,{children:"true"}),", then add ",(0,c.jsx)(n.code,{children:"Redis connection information"})," to the ",(0,c.jsx)(n.code,{children:".env"})," if you are using it. otherwise, you can use an external tool to inject these variables into the environment"]}),"\n",(0,c.jsxs)(n.p,{children:["Here is a sample of the ",(0,c.jsx)(n.code,{children:"Redis connection information in the "}),".env` file:"]}),"\n",(0,c.jsx)(n.pre,{children:(0,c.jsx)(n.code,{className:"language-bash",children:"#######################################\n######            CACHE          ######\n#######################################\nCACHE_DRIVER=redis\nREDIS_HOST=localhost\nREDIS_PORT=6379\nREDIS_PASSWORD=\nREDIS_DB=0\n"})}),"\n",(0,c.jsx)(n.h4,{id:"set-values-in-the-cache",children:"Set values in the cache"}),"\n",(0,c.jsx)(n.pre,{children:(0,c.jsx)(n.code,{className:"language-go",children:'package controllers\n\nimport (\n    "git.smarteching.com/goffee/core"\n)\n\nfunc Login(c *core.Context) *core.Response {\n    err := c.GetCache().Set("userID", 12345)\n}\n'})}),"\n",(0,c.jsx)(n.h4,{id:"set-values-in-the-cache-with-an-expiration-date",children:"Set values in the cache with an expiration date"}),"\n",(0,c.jsx)(n.pre,{children:(0,c.jsx)(n.code,{className:"language-go",children:'package controllers\n\nimport (\n    "git.smarteching.com/goffee/core"\n)\n\nfunc Login(c *core.Context) *core.Response {\n    hours24 := time.Duration(time.Hour * 24) // 24 hours duration\n    afterHours24: time.Now().Add(hours24) // the date after 24 hours from now\n    err := c.GetCache().SetWithExpiration("userID", 12345, afterHours24) // expires after 24 hours\n}\n'})}),"\n",(0,c.jsx)(n.h4,{id:"get-values-from-cache",children:"Get values from cache"}),"\n",(0,c.jsx)(n.p,{children:"Here is how you can get values from cache"}),"\n",(0,c.jsx)(n.pre,{children:(0,c.jsx)(n.code,{className:"language-go",children:'package controllers\n\nimport (\n    "git.smarteching.com/goffee/core"\n)\n\nfunc Login(c *core.Context) *core.Response {\n    userID, err := c.GetCache().Get("userID")\n}\n'})}),"\n",(0,c.jsx)(n.h4,{id:"cache-delete",children:"Cache Delete"}),"\n",(0,c.jsx)(n.p,{children:"Here is how you can delete something from the cache"}),"\n",(0,c.jsx)(n.pre,{children:(0,c.jsx)(n.code,{className:"language-go",children:'package controllers\n\nimport (\n    "git.smarteching.com/goffee/core"\n)\n\nfunc Login(c *core.Context) *core.Response {\n    err := c.GetCache().Delete("userID")\n}\n'})})]})}function d(e={}){const{wrapper:n}={...(0,o.R)(),...e.components};return n?(0,c.jsx)(n,{...e,children:(0,c.jsx)(l,{...e})}):l(e)}},8453:(e,n,t)=>{t.d(n,{R:()=>i,x:()=>r});var c=t(6540);const o={},a=c.createContext(o);function i(e){const n=c.useContext(a);return c.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function r(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:i(e.components),c.createElement(a.Provider,{value:n},e.children)}}}]);