如何高效率存储微信中的 access_token 限制申请2000次/天

论坛 期权论坛     
匿名小用户   2019-10-20 18:27   209   0
<!-- flowchart 箭头图标 勿删 -->
                    <svg style="display: none;">
                        <path d="M5,0 0,2.5 5,5z" id="raphael-marker-block" stroke-linecap="round" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                                            <h1 id="如何高效率存储微信中的-accesstoken"><strong>如何高效率存储微信中的 access_token</strong></h1>

<hr>

<blockquote>
  <p>众所周知,在微信开发中,获取<code>access_token</code> 的接口每天的调用次数是有限制的,<code>2000</code>次应该是。 <br>
  不过其实这些完全够用了,除非你不小心写了个循环,在1秒中内用完了。 <br>
  每个<code>access_token</code> 的生效时间是<code>2小时内</code>,2小时过后就需要重新申请一下。其实每天只需要申请12次就可以了。 <br>
  不管你怎么申请,当前申请了<code>access_token</code>,以前的就都不能用了。</p>
</blockquote>

<p>所以解决办法就是: <br>
在数据库中新建一个表<code>token</code>盛放申请来的<code>access_token</code>,字段有三个</p>

<pre class="blockcode"><code class="language-bash hljs">access_token <span class="hljs-comment">#存放access_token</span>
expires <span class="hljs-comment">#存放毫秒数</span>
update_time <span class="hljs-comment">#更新时间戳</span></code></pre>



<h2 id="逻辑是这样的表中只有一条数据"><strong>逻辑是这样的:</strong>(表中只有一条数据)</h2>

<pre class="blockcode"><code class="hljs">查询表中数据
    如果有数据,用 时间戳+更新时间 跟 现在时间比较,计算是否已过期:
        如果已过期,重新申请,并更新数据库中数据,并返回access_token
        如果没有过期,直接返回查询出的access_token
    如果没数据:
        申请数据并,插入数据库,返回access_tooken</code></pre>

<div class="flow-chart"><svg height="792.75" style="overflow: hidden; position: relative; top: -0.3125px;" version="1.1" width="835.5234375"><desc style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">Created with Raphal 2.1.0</desc><defs style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><path d="M5,0 0,2.5 5,5z" id="raphael-marker-block" stroke-linecap="round" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path><marker id="raphael-marker-endblock33" markerheight="3" markerwidth="3" orient="auto" refx="1.5" refy="1.5" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><use fill="black" href="#raphael-marker-block" stroke="none" stroke-width="1.6667" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);" transform="rotate(180 1.5 1.5) scale(0.6,0.6)" xlink="http://www.w3.org/1999/xlink"></use></marker></defs><rect class="flowchart" fill="#ffffff" height="39" id="st" r="20" rx="20" ry="20" stroke="#000000" stroke-width="2" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);" transform="matrix(1,0,0,1,69.0703,40.75)" width="110" x="0" y="0"></rect><text class="flowchartt" fill="#000000" font="10px &quot;Arial&quot;" font-family="sans-serif" font-size="14px" font-weight="normal" id="stt" stroke="none" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); text-anchor: start; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: sans-serif;" text-anchor="start" transform="matrix(1,0,0,1,69.0703,40.75)" x="10" y="19.5"><tspan dy="5.5" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">查询表中数据</tspan></text><path class="flowchart" d="M45,22.5L0,45L90,90L180,45L90,0L0,45" fill="#ffffff" font-family="sans-serif" font-weight="normal" id="cond1" stroke="#000000" stroke-width="2" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: sans-serif; font-weight: normal;" transform="matrix(1,0,0,1,34.0703,145)"></path><text class="flowchartt" fill="#000000" font="10px &quot;Arial&quot;" font-family="sans-serif" font-size="14px" font-weight="normal" id="cond1t" stroke="none" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); text-anchor: start; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: sans-serif;" text-anchor="start" transform="matrix(1,0,0,1,34.0703,145)" x="50" y="45"><tspan dy="5.5" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">是否有数据?</tspan></text><path class="flowchart" d="M56.25,28.125L0,56.25L112.5,112.5L225,56.25L112.5,0L0,56.25" fill="#ffffff" font-family="sans-serif" font-weight="normal" id="cond2" stroke="#000000" stroke-width="2" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: sans-serif; font-weight: normal;" transform="matrix(1,0,0,1,11.5703,289)"></path><text class="flowchartt" fill="#000000" font="10px &quot;Arial&quot;" font-family="sans-serif" font-size="14px" font-weight="normal" id="cond2t" stroke="none" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); text-anchor: start; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: sans-serif;" text-anchor="start" transform="matrix(1,0,0,1,11.5703,289)" x="61.25" y="56.25"><tspan dy="5.5" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">查询数据是否过期</tspan></text><rect class="flowchart" fill="#ffffff" height="39" id="op
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:50
帖子:1079
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP