win10 Redis的安装使用及配置

原文地址

下载安装

Redis–Window版是GitHub上的一个开源项目我们可以直接下载解压使用。
Download Now

在D盘下新建Redis文件(这个路径可以自定义),将Redis解压至该文件

《win10 Redis的安装使用及配置》

安装完后 打开 win控制台 cd 进入 Redis 目录
运行:redis-server.exe redis.windows.conf
如果出现以下信息则说明安装成功。

《win10 Redis的安装使用及配置》

配置

配置之前先关掉Redis服务

添加启动Redis的快捷方式

为redis-server.exe创建快捷方式redis(名字 自定义)

修改redis的属性,为redis的目标属性,加入并指定读取的配置文件redis.windows.conf,前面要加空格。

通过快捷方式redis启动Redis服务

《win10 Redis的安装使用及配置》

修改Redis的默认配置

设置Redis的最大占用内存

Redis设置最大占用内存,打开redis配置文件,找到如下段落,设置maxmemory参数,maxmemory是bytes字节类型,注意转换。修改如下所示:

# NOTE: since Redis uses the system paging file to allocate the heap memory,
# the Working Set memory usage showed by the Windows Task Manager or by other
# tools such as ProcessExplorer will not always be accurate. For example, right
# after a background save of the RDB or the AOF files, the working set value
# may drop significantly. In order to check the correct amount of memory used
# by the redis-server to store the data, use the INFO client command. The INFO
# command shows only the memory used to store the redis data, not the extra
# memory used by the Windows process for its own requirements. Th3 extra amount
# of memory not reported by the INFO command can be calculated subtracting the
# Peak Working Set reported by the Windows Task Manager and the used_memory_peak
# reported by the INFO command.
#
# maxmemory <bytes>  根据自己需求配置
maxmemory 2147483648    

如果不设置maxmemory或者设置为0,64位系统不限制内存,32位系统最多使用3GB内存。

修改数据默认存放位置

这里自定义一个文件夹,用来存放Redis数据

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir D:\MyRedisSwap

设置LRU算法删除数据

设置了maxmemory的选项,redis内存使用达到上限。可以通过设置LRU算法来删除部分key,释放空间。默认是按照过期时间的,如果set时候没有加上过期时间就会导致数据写满maxmemory。

LRU是Least Recently Used 近期最少使用算法。

  • volatile-lru -> 根据LRU算法生成的过期时间来删除。
  • -allkeys-lru -> 根据LRU算法删除任何key。
  • volatile-random -> 根据过期设置来随机删除key。
  • allkeys->random -> 无差别随机删。
  • volatile-ttl -> 根据最近过期时间来删除(辅以TTL)
  • noeviction -> 谁也不删,直接在写操作时返回错误。

如果设置了maxmemory,一般都要设置过期策略。Redis默认有六种过期策略

# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select among five behaviors:
#
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key according to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations
#
# Note: with any of the above policies, Redis will return an error on write
#       operations, when there are no suitable keys for eviction.
#
#       At the date of writing these commands are: set setnx setex append
#       incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
#       sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
#       zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
#       getset mset msetnx exec sort
#
# The default is:
#
# maxmemory-policy noeviction
maxmemory-policy volatile-lru

以下是其他配置:

Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 (在Windows上不支持守护进程)

daemonize no 

指定Redis监听端口,默认端口为6379
(在Windows上不支持守护进程)

 port 6379

绑定的主机地址

bind 127.0.0.1

指定在多长时间内,有多少次更新操作,就将数据同步到数据文件,可以多个条件配合

save <seconds> <changes>

Redis默认配置文件中提供了三个条件:

save 900 1
save 300 10
save 60 10000

分别表示900秒(15分钟)内有1个更改,300秒(5分钟)内有10个更改以及60秒内有10000个更改。

指定更新日志文件名,默认为appendonly.aof

appendfilename appendonly.aof

指定更新日志条件,共有3个可选值:

no:表示等操作系统进行数据缓存同步到磁盘(快) 
always:表示每次更新操作后手动调用fsync()将数据写到磁盘(慢,安全) 
everysec:表示每秒同步一次(折衷,默认值)

appendfsync everysec

指定在超过一定的数量或者最大的元素超过某一临界值时,采用一种特殊的哈希算法

hash-max-zipmap-entries 64
hash-max-zipmap-value 512

参考链接:

Redis中文网

    原文作者:gyl_coder
    原文地址: https://segmentfault.com/a/1190000014651996
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞