缓存
- 什么是缓存
高速设备代替低速设备承载更多处理能力和IO操作。
- Redis是一个内存性数据库,用于缓存硬盘数据,进而提升性能。
- 为什么需要缓存:
硬盘太慢了,所以让内存多做些事情;内存比硬盘要快至少100倍,通过缓存可以更好支撑高并发。
哈喽,大家好,我是有勇气的牛排(全网同名)🐮🐮🐮
有问题的小伙伴欢迎在文末评论,点赞、收藏是对我最大的支持!!!。
目录
1 连接数据库
pip install redis
import redis
def redis_connect():
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, decode_responses=True, db=0)
red = redis.Redis(connection_pool=pool)
return red
red = redis_connect()
red.hset('bigkey', 'smallkey', 'value')
2 操作
2.1 string类型
set(name, value, ex=None, px=None, nx=False, xx=False)
ex
5分钟
:60 * 5 = 300
一天
:3600 * 24 = 86400
一周
:3600 * 24 * 7 = 604800
一个月
:3600 * 24 * 30 = 2592000
2.1.1 新增 and 修改
r.set('a','666')
ex
: 失效时间
r.set('a','666',ex=3000)
2.1.2 删
-
删除数据库所有key
r.flushdb()
-
删除指定key
r.delete('a')
2.1.3 查
-
获取指定key值
r.get('a')
-
获取所有以 a 开头的 key
r.keys('a*')
2.2 哈希(hash)类型
2.2.1 新增 and 修改
参数一:大key
参数儿:小key
r.hset('bigKey', 'smallKey', '{"age":18,"addr":"北京"}')
2.2.2 获取指定 value 值
.hget('大key','小key')
res = r.hget('bigKey', 'smallKey')
print(res)
输出:

2.2.3 获取所有 小key
.hgetall('大key')
res = r.hgetall('bigKey')
print(res)
2.2.4 删除 大key
r.delete('bigKey')
2.2.5 删除 小key
r.hdel('bigKey','smallKey')
参考文章:
https://www.cnblogs.com/yiruliu/p/9798219.html
https://segmentfault.com/a/1190000014416025
<h2><a id="_0"></a>缓存</h2>
<ol>
<li><strong>什么是缓存</strong><br />
高速设备代替低速设备承载更多处理能力和IO操作。</li>
<li><strong>Redis</strong>是一个内存性数据库,用于缓存硬盘数据,进而提升性能。</li>
<li>为什么需要缓存:<br />
硬盘太慢了,所以让内存多做些事情;内存比硬盘要快至少100倍,通过缓存可以更好支撑高并发。</li>
</ol>
<p><font face="楷体,华文行楷,隶书,黑体" color="red" size="4"><strong>哈喽,大家好,我是有勇气的牛排(全网同名)🐮🐮🐮</strong></font></p>
<p><font face="楷体,华文行楷,隶书,黑体" color="blue" size="4"><strong>有问题的小伙伴欢迎在文末评论,点赞、收藏是对我最大的支持!!!。</strong></font></p>
<p><h3>目录</h3><ul><ul><li><a href="#_0">缓存</a></li><li><a href="#1__13">1 连接数据库</a></li><li><a href="#2__33">2 操作</a></li><ul><li><a href="#21_string_34">2.1 string类型</a></li><li><a href="#211__and__50">2.1.1 新增 and 修改</a></li><li><a href="#212__60">2.1.2 删</a></li><li><a href="#213__73">2.1.3 查</a></li></ul><li><a href="#22_hash_86">2.2 哈希(hash)类型</a></li><ul><li><a href="#221__and__87">2.2.1 新增 and 修改</a></li><li><a href="#222__value__94">2.2.2 获取指定 value 值</a></li><li><a href="#223__key_106">2.2.3 获取所有 小key</a></li><li><a href="#224__key_114">2.2.4 删除 大key</a></li><li><a href="#225__key_120">2.2.5 删除 小key</a></li></ul></ul></ul></p>
<h2><a id="1__13"></a>1 连接数据库</h2>
<pre><div class="hljs"><code class="lang-powershell">pip install redis
</code></div></pre>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">import</span> redis
<span class="hljs-keyword">def</span> <span class="hljs-title function_">redis_connect</span>():
<span class="hljs-comment"># redis-py使用connection pool来管理对一个redis server的所有连接,避免每次建立、释放连接的开销。默认,每个Redis实例都会维护一个自己的连接池。</span>
pool = redis.ConnectionPool(host=<span class="hljs-string">'127.0.0.1'</span>, port=<span class="hljs-number">6379</span>, decode_responses=<span class="hljs-literal">True</span>, db=<span class="hljs-number">0</span>)
red = redis.Redis(connection_pool=pool)
<span class="hljs-keyword">return</span> red
<span class="hljs-comment"># 连接到Redis服务器</span>
red = redis_connect()
red.hset(<span class="hljs-string">'bigkey'</span>, <span class="hljs-string">'smallkey'</span>, <span class="hljs-string">'value'</span>)
</code></div></pre>
<h2><a id="2__33"></a>2 操作</h2>
<h3><a id="21_string_34"></a>2.1 string类型</h3>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-built_in">set</span>(name, value, ex=<span class="hljs-literal">None</span>, px=<span class="hljs-literal">None</span>, nx=<span class="hljs-literal">False</span>, xx=<span class="hljs-literal">False</span>)
<span class="hljs-comment"># ex,过期时间(秒)</span>
<span class="hljs-comment"># px,过期时间(毫秒)</span>
<span class="hljs-comment"># nx,如果设置为True,则只有name不存在时,当前set操作才执行</span>
<span class="hljs-comment"># xx,如果设置为True,则只有name存在时,当前set操作才执行</span>
</code></div></pre>
<p>ex<br />
<code>5分钟</code>:60 * 5 = 300<br />
<code>一天</code>:3600 * 24 = 86400<br />
<code>一周</code>:3600 * 24 * 7 = 604800<br />
<code>一个月</code>:3600 * 24 * 30 = 2592000</p>
<h3><a id="211__and__50"></a>2.1.1 新增 and 修改</h3>
<pre><div class="hljs"><code class="lang-powershell"> r.set(<span class="hljs-string">'a'</span>,<span class="hljs-string">'666'</span>)
</code></div></pre>
<p><code>ex</code>: 失效时间</p>
<pre><div class="hljs"><code class="lang-powershell"> r.set(<span class="hljs-string">'a'</span>,<span class="hljs-string">'666'</span>,ex=<span class="hljs-number">3000</span>)
</code></div></pre>
<h3><a id="212__60"></a>2.1.2 删</h3>
<ol>
<li>
<p>删除数据库所有key</p>
<pre><div class="hljs"><code class="lang-powershell">r.flushdb()
</code></div></pre>
</li>
<li>
<p>删除指定key</p>
<pre><div class="hljs"><code class="lang-powershell">r.delete(<span class="hljs-string">'a'</span>)
</code></div></pre>
</li>
</ol>
<h3><a id="213__73"></a>2.1.3 查</h3>
<ol>
<li>
<p>获取指定key值</p>
<pre><div class="hljs"><code class="lang-powershell">r.get(<span class="hljs-string">'a'</span>)
</code></div></pre>
</li>
<li>
<p>获取所有以 a 开头的 key</p>
<pre><div class="hljs"><code class="lang-powershell">r.keys(<span class="hljs-string">'a*'</span>)
</code></div></pre>
</li>
</ol>
<h2><a id="22_hash_86"></a>2.2 哈希(hash)类型</h2>
<h3><a id="221__and__87"></a>2.2.1 新增 and 修改</h3>
<p>参数一:大key<br />
参数儿:小key</p>
<pre><div class="hljs"><code class="lang-powershell">r.hset(<span class="hljs-string">'bigKey'</span>, <span class="hljs-string">'smallKey'</span>, <span class="hljs-string">'{"age":18,"addr":"北京"}'</span>)
</code></div></pre>
<h3><a id="222__value__94"></a>2.2.2 获取指定 value 值</h3>
<p><code>.hget('大key','小key')</code></p>
<pre><div class="hljs"><code class="lang-powershell">res = r.hget(<span class="hljs-string">'bigKey'</span>, <span class="hljs-string">'smallKey'</span>)
print(res)
</code></div></pre>
<p>输出:</p>
<p><img src="https://static.couragesteak.com/article/a8c23b794792531cdc228d4a53f1e5cb.png" alt="image.png" /></p>
<h3><a id="223__key_106"></a>2.2.3 获取所有 小key</h3>
<p><code>.hgetall('大key')</code></p>
<pre><div class="hljs"><code class="lang-powershell">res = r.hgetall(<span class="hljs-string">'bigKey'</span>)
print(res)
</code></div></pre>
<h3><a id="224__key_114"></a>2.2.4 删除 大key</h3>
<pre><div class="hljs"><code class="lang-powershell">r.delete(<span class="hljs-string">'bigKey'</span>)
</code></div></pre>
<h3><a id="225__key_120"></a>2.2.5 删除 小key</h3>
<pre><div class="hljs"><code class="lang-powershell">r.hdel(<span class="hljs-string">'bigKey'</span>,<span class="hljs-string">'smallKey'</span>)
</code></div></pre>
<p>参考文章:<br />
https://www.cnblogs.com/yiruliu/p/9798219.html<br />
https://segmentfault.com/a/1190000014416025</p>
留言