- 浏览: 2265504 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (423)
- FileNet相关 (3)
- 应用服务器相关 (22)
- Java综合 (77)
- 持久层 (24)
- struts (11)
- webservice (8)
- 虚拟机 (2)
- 光盘刻录 (0)
- AD及AD集群 (1)
- JS (33)
- F5 (0)
- loadrunner8.1 (0)
- Java 反编译工具 (2)
- DataBase (62)
- ant (1)
- 操作系统 (29)
- 我的任务 (3)
- 平台架构 (16)
- 业务规则引擎 (2)
- 模板 (1)
- EJB (5)
- spring (24)
- CMMI (1)
- 项目管理 (20)
- LDAP (13)
- JMS (10)
- JSP (19)
- JBPM (2)
- web MVC框架设计思想 (2)
- 第三方支付平台 (2)
- BUG管理工具 (1)
- 垃圾站 (2)
- php (1)
- swing (1)
- 书籍 (1)
- QQ qq (2)
- 移动互联网 (26)
- 爱听的歌曲 (0)
- hadoop (4)
- 数据库 (9)
- 设计模式 (1)
- 面试经验只谈 (1)
- 大数据 (9)
- sp (1)
- 缓存数据库 (8)
- storm (2)
- taobao (2)
- 分布式,高并发,大型互联网,负载均衡 (6)
- Apache Ignite (0)
- Docker & K8S (0)
最新评论
-
wangyudong:
新版本 Wisdom RESTClienthttps://gi ...
spring rest mvc使用RestTemplate调用 -
wangyudong:
很多API doc生成工具生成API文档需要引入第三方依赖,重 ...
spring rest mvc使用RestTemplate调用 -
zhaoshijie:
cfying 写道大侠,还是加载了两次,怎么解决啊?求。QQ: ...
spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件) -
xinglianxlxl:
对我有用,非常感谢
spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件) -
k_caesar:
多谢,学习了
利用maven的resources、filter和profile实现不同环境使用不同配置文件
关键字:redis for Windows
一、windows下安装redis
1:首先下载redis:redis-2.0.2.zip (32 bit),解压。
从下面地址下:http://code.google.com/p/servicestack/wiki/RedisWindowsDownload,看到下面有redis-2.0.2.zip (32 bit),就是他了,下载完成后,解压到D:\redis-2.0.2.
2:创建redis.conf文件:
这是一个配置文件,指定了redis的监听端口,timeout等。如下面有:port 6379。
把下面内容COPY到一新建文件中,取名redis.conf,再保存到redis-2.0.2目录下:
# Redis configuration file example
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize no
# When run as a daemon, Redis write a pid file in /var/run/redis.pid by default.
# You can specify a custom pid file location here.
pidfile /var/run/redis.pid
# Accept connections on the specified port, default is 6379
port 6379
# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for connections.
#
# bind 127.0.0.1
# Close the connection after a client is idle for N seconds (0 to disable)
timeout 300
# Set server verbosity to 'debug'
# it can be one of:
# debug (a lot of information, useful for development/testing)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel debug
# Specify the log file name. Also 'stdout' can be used to force
# the demon to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile stdout
# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
databases 16
################################ SNAPSHOTTING #################################
#
# Save the DB on disk:
#
# save <seconds> <changes>
#
# Will save the DB if both the given number of seconds and the given
# number of write operations against the DB occurred.
#
# In the example below the behaviour will be to save:
# after 900 sec (15 min) if at least 1 key changed
# after 300 sec (5 min) if at least 10 keys changed
# after 60 sec if at least 10000 keys changed
save 900 1
save 300 10
save 60 10000
# Compress string objects using LZF when dump .rdb databases?
# For default that's set to 'yes' as it's almost always a win.
# If you want to save some CPU in the saving child set it to 'no' but
# the dataset will likely be bigger if you have compressible values or keys.
rdbcompression yes
# The filename where to dump the DB
dbfilename dump.rdb
# For default save/load DB in/from the working directory
# Note that you must specify a directory not a file name.
dir ./
################################# REPLICATION #################################
# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. Note that the configuration is local to the slave
# so for example it is possible to configure the slave to save the DB with a
# different interval, or to listen to another port, and so on.
#
# slaveof <masterip> <masterport>
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
# masterauth <master-password>
################################## SECURITY ###################################
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands. This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# requirepass foobared
################################### LIMITS ####################################
# Set the max number of connected clients at the same time. By default there
# is no limit, and it's up to the number of file descriptors the Redis process
# is able to open. The special value '0' means no limts.
# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
#
# maxclients 128
# Don't use more memory than the specified amount of bytes.
# When the memory limit is reached Redis will try to remove keys with an
# EXPIRE set. It will try to start freeing keys that are going to expire
# in little time and preserve keys with a longer time to live.
# Redis will also try to remove objects from free lists if possible.
#
# If all this fails, Redis will start to reply with errors to commands
# that will use more memory, like SET, LPUSH, and so on, and will continue
# to reply to most read-only commands like GET.
#
# WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
# 'state' server or cache, not as a real DB. When Redis is used as a real
# database the memory usage will grow over the weeks, it will be obvious if
# it is going to use too much memory in the long run, and you'll have the time
# to upgrade. With maxmemory after the limit is reached you'll start to get
# errors for write operations, and this may even lead to DB inconsistency.
#
# maxmemory <bytes>
############################## APPEND ONLY MODE ###############################
# By default Redis asynchronously dumps the dataset on disk. If you can live
# with the idea that the latest records will be lost if something like a crash
# happens this is the preferred way to run Redis. If instead you care a lot
# about your data and don't want to that a single record can get lost you should
# enable the append only mode: when this mode is enabled Redis will append
# every write operation received in the file appendonly.log. This file will
# be read on startup in order to rebuild the full dataset in memory.
#
# Note that you can have both the async dumps and the append only file if you
# like (you have to comment the "save" statements above to disable the dumps).
# Still if append only mode is enabled Redis will load the data from the
# log file at startup ignoring the dump.rdb file.
#
# The name of the append only file is "appendonly.log"
#
# IMPORTANT: Check the BGREWRITEAOF to check how to rewrite the append
# log file in background when it gets too big.
appendonly no
# The fsync() call tells the Operating System to actually write data on disk
# instead to wait for more data in the output buffer. Some OS will really flush
# data on disk, some other OS will just try to do it ASAP.
#
# Redis supports three different modes:
#
# no: don't fsync, just let the OS flush the data when it wants. Faster.
# always: fsync after every write to the append only log . Slow, Safest.
# everysec: fsync only if one second passed since the last fsync. Compromise.
#
# The default is "always" that's the safer of the options. It's up to you to
# understand if you can relax this to "everysec" that will fsync every second
# or to "no" that will let the operating system flush the output buffer when
# it want, for better performances (but if you can live with the idea of
# some data loss consider the default persistence mode that's snapshotting).
appendfsync always
# appendfsync everysec
# appendfsync no
############################### ADVANCED CONFIG ###############################
# Glue small output buffers together in order to send small replies in a
# single TCP packet. Uses a bit more CPU but most of the times it is a win
# in terms of number of queries per second. Use 'yes' if unsure.
glueoutputbuf yes
# Use object sharing. Can save a lot of memory if you have many common
# string in your dataset, but performs lookups against the shared objects
# pool so it uses more CPU and can be a bit slower. Usually it's a good
# idea.
#
# When object sharing is enabled (shareobjects yes) you can use
# shareobjectspoolsize to control the size of the pool used in order to try
# object sharing. A bigger pool size will lead to better sharing capabilities.
# In general you want this value to be at least the double of the number of
# very common strings you have in your dataset.
#
# WARNING: object sharing is experimental, don't enable this feature
# in production before of Redis 1.0-stable. Still please try this feature in
# your development environment so that we can test it better.
# shareobjects no
# shareobjectspoolsize 1024
3:在cmd下面执行以下命令,指定它使用我们的redis.conf,同时也是启动,把redis运行起来,这里指定用redis.conf的配置运行服务器
D:\redis-2.0.2>redis-server.exe redis.conf
4:开一新DOS窗口cmd.执行以下命令,这是Redis的客户端程序:
redis-cli.exe -h 172.18.5.1 -p 6379
172.18.5.1是我本机IP地址,端口6379就是上面配置文件中指定的监听端口
执行完成后,应该能看到redis启动了,这时在第一个cmd窗口可以看到连接信息。
执行一条保存key value操作
set mystock 300156
再查询一下
get mystock
=================================
可以完全参考以下地址的资料:
http://hi.baidu.com/zchare/blog/item/bd6034f325d1c65f352acca1.html(very good,安全可以成功)
http://cardyn.iteye.com/blog/794194
http://zhaohaolin.iteye.com/blog/1017561
二、redis配置选项:
指定redis的配置文件,如没有指定,则使用默认设置
D:\redis-2.0.0-rc2>redis-server.exe redis.conf
redis.conf配置选项如下
daemonize 是否以后台进程运行,默认为no
pidfile 如以后台进程运行,则需指定一个pid,默认为/var/run/redis.pid
bind 绑定主机IP,默认值为127.0.0.1(注释)
port 监听端口,默认为6379
timeout 超时时间,默认为300(秒)
loglevel 日志记录等级,有4个可选值,debug,verbose(默认值),notice,warning
logfile 日志记录方式,默认值为stdout
databases 可用数据库数,默认值为16,默认数据库为0
save <seconds> <changes> 指出在多长时间内,有多少次更新操作,就将数据同步到数据文件。这个可以多个条件配合,比如默认配置文件中的设置,就设置了三个条件。
save 900 1 900秒(15分钟)内至少有1个key被改变
save 300 10 300秒(5分钟)内至少有300个key被改变
save 60 10000 60秒内至少有10000个key被改变
rdbcompression 存储至本地数据库时是否压缩数据,默认为yes
dbfilename 本地数据库文件名,默认值为dump.rdb
dir 本地数据库存放路径,默认值为 ./
slaveof <masterip> <masterport> 当本机为从服务时,设置主服务的IP及端口(注释)
masterauth <master-password> 当本机为从服务时,设置主服务的连接密码(注释)
requirepass 连接密码(注释)
maxclients 最大客户端连接数,默认不限制(注释)
maxmemory <bytes> 设置最大内存,达到最大内存设置后,Redis会先尝试清除已到期或即将到期的Key,当此方法处理后,任到达最大内存设置,将无法再进行写入操作。(注释)
appendonly 是否在每次更新操作后进行日志记录,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为redis本身同步数据文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认值为no
appendfilename 更新日志文件名,默认值为appendonly.aof(注释)
appendfsync 更新日志条件,共有3个可选值。no表示等操作系统进行数据缓存同步到磁盘,always表示每次更新操作后手动调用fsync()将数据写到磁盘,everysec表示每秒同步一次(默认值)。
vm-enabled 是否使用虚拟内存,默认值为no
vm-swap-file 虚拟内存文件路径,默认值为/tmp/redis.swap,不可多个Redis实例共享
vm-max-memory 将所有大于vm-max-memory的数据存入虚拟内存,无论vm-max-memory设置多小,所有索引数据都是内存存储的(Redis的索引数据就是keys),也就是说,当vm-max-memory设置为0的时候,其实是所有value都存在于磁盘。默认值为0。
Redis官方文档对VM的使用提出了一些建议:
当你的key很小而value很大时,使用VM的效果会比较好.因为这样节约的内存比较大.
当你的key不小时,可以考虑使用一些非常方法将很大的key变成很大的value,比如你可以考虑将key,value组合成一个新的value.
最好使用linux ext3 等对稀疏文件支持比较好的文件系统保存你的swap文件.
vm-max-threads这个参数,可以设置访问swap文件的线程数,设置最好不要超过机器的核数.如果设置为0,那么所有对swap文件的操作都是串行的.可能会造成比较长时间的延迟,但是对数据完整性有很好的保证.redis-cli.exe:命令行客户端,测试用
D:\redis-2.0.0-rc2>redis-cli.exe -h 127.0.0.1 -p 6379
三、redis java操作(包括list/Map/删除/新增/覆盖/排序等都支持)
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import javax.sound.midi.Soundbank;
import java.util.*;
/**
* @author: flychao88
* Time: 2012.5.7 16:23:15
*/
public class RedisTest {
JedisPool pool;
Jedis jedis;
@Before
public void setUp() {
pool = new JedisPool(new JedisPoolConfig(), "172.16.100.184");
jedis = pool.getResource();
jedis.auth("password");
}
/**
* Redis存储初级的字符串
* CRUD
*/
@Test
public void testBasicString(){
//-----添加数据----------
jedis.set("name","minxr");//向key-->name中放入了value-->minxr
System.out.println(jedis.get("name"));//执行结果:minxr
//-----修改数据-----------
//1、在原来基础上修改
jedis.append("name","jarorwar"); //很直观,类似map 将jarorwar append到已经有的value之后
System.out.println(jedis.get("name"));//执行结果:minxrjarorwar
//2、直接覆盖原来的数据
jedis.set("name","闵晓荣");
System.out.println(jedis.get("name"));//执行结果:闵晓荣
//删除key对应的记录
jedis.del("name");
System.out.println(jedis.get("name"));//执行结果:null
/**
* mset相当于
* jedis.set("name","minxr");
* jedis.set("jarorwar","闵晓荣");
*/
jedis.mset("name","minxr","jarorwar","闵晓荣");
System.out.println(jedis.mget("name","jarorwar"));
}
/**
* jedis操作Map
*/
@Test
public void testMap(){
Map<String,String> user=new HashMap<String,String>();
user.put("name","minxr");
user.put("pwd","password");
jedis.hmset("user",user);
//取出user中的name,执行结果:[minxr]-->注意结果是一个泛型的List
//第一个参数是存入redis中map对象的key,后面跟的是放入map中的对象的key,后面的key可以跟多个,是可变参数
List<String> rsmap = jedis.hmget("user", "name");
System.out.println(rsmap);
//删除map中的某个键值
// jedis.hdel("user","pwd");
System.out.println(jedis.hmget("user", "pwd")); //因为删除了,所以返回的是null
System.out.println(jedis.hlen("user")); //返回key为user的键中存放的值的个数1
System.out.println(jedis.exists("user"));//是否存在key为user的记录 返回true
System.out.println(jedis.hkeys("user"));//返回map对象中的所有key [pwd, name]
System.out.println(jedis.hvals("user"));//返回map对象中的所有value [minxr, password]
Iterator<String> iter=jedis.hkeys("user").iterator();
while (iter.hasNext()){
String key = iter.next();
System.out.println(key+":"+jedis.hmget("user",key));
}
}
/**
* jedis操作List
*/
@Test
public void testList(){
//开始前,先移除所有的内容
jedis.del("java framework");
System.out.println(jedis.lrange("java framework",0,-1));
//先向key java framework中存放三条数据
jedis.lpush("java framework","spring");
jedis.lpush("java framework","struts");
jedis.lpush("java framework","hibernate");
//再取出所有数据jedis.lrange是按范围取出,
// 第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有
System.out.println(jedis.lrange("java framework",0,-1));
}
/**
* jedis操作Set
*/
@Test
public void testSet(){
//添加
jedis.sadd("sname","minxr");
jedis.sadd("sname","jarorwar");
jedis.sadd("sname","闵晓荣");
jedis.sadd("sanme","noname");
//移除noname
jedis.srem("sname","noname");
System.out.println(jedis.smembers("sname"));//获取所有加入的value
System.out.println(jedis.sismember("sname", "minxr"));//判断 minxr 是否是sname集合的元素
System.out.println(jedis.srandmember("sname"));
System.out.println(jedis.scard("sname"));//返回集合的元素个数
}
@Test
public void test() throws InterruptedException {
//keys中传入的可以用通配符
System.out.println(jedis.keys("*")); //返回当前库中所有的key [sose, sanme, name, jarorwar, foo, sname, java framework, user, braand]
System.out.println(jedis.keys("*name"));//返回的sname [sname, name]
System.out.println(jedis.del("sanmdde"));//删除key为sanmdde的对象 删除成功返回1 删除失败(或者不存在)返回 0
System.out.println(jedis.ttl("sname"));//返回给定key的有效时间,如果是-1则表示永远有效
jedis.setex("timekey", 10, "min");//通过此方法,可以指定key的存活(有效时间) 时间为秒
Thread.sleep(5000);//睡眠5秒后,剩余时间将为<=5
System.out.println(jedis.ttl("timekey")); //输出结果为5
jedis.setex("timekey", 1, "min"); //设为1后,下面再看剩余时间就是1了
System.out.println(jedis.ttl("timekey")); //输出结果为1
System.out.println(jedis.exists("key"));//检查key是否存在
System.out.println(jedis.rename("timekey","time"));
System.out.println(jedis.get("timekey"));//因为移除,返回为null
System.out.println(jedis.get("time")); //因为将timekey 重命名为time 所以可以取得值 min
//jedis 排序
//注意,此处的rpush和lpush是List的操作。是一个双向链表(但从表现来看的)
jedis.del("a");//先清除数据,再加入数据进行测试
jedis.rpush("a", "1");
jedis.lpush("a","6");
jedis.lpush("a","3");
jedis.lpush("a","9");
System.out.println(jedis.lrange("a",0,-1));// [9, 3, 6, 1]
System.out.println(jedis.sort("a")); //[1, 3, 6, 9] //输入排序后结果
System.out.println(jedis.lrange("a",0,-1));
}
六、redis和memcached比较
1、memcached多线程 redis单线程
2、memcached支持简单的key/value redis支持最为常用的数据类型主要由五种:String、Hash、List、Set和Sorted Set(最吸引人的就此,而且可以对list排序等)
3、memcached客户端实现分布式 redis服务器端实现分布式
4、
一、windows下安装redis
1:首先下载redis:redis-2.0.2.zip (32 bit),解压。
从下面地址下:http://code.google.com/p/servicestack/wiki/RedisWindowsDownload,看到下面有redis-2.0.2.zip (32 bit),就是他了,下载完成后,解压到D:\redis-2.0.2.
2:创建redis.conf文件:
这是一个配置文件,指定了redis的监听端口,timeout等。如下面有:port 6379。
把下面内容COPY到一新建文件中,取名redis.conf,再保存到redis-2.0.2目录下:
# Redis configuration file example
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize no
# When run as a daemon, Redis write a pid file in /var/run/redis.pid by default.
# You can specify a custom pid file location here.
pidfile /var/run/redis.pid
# Accept connections on the specified port, default is 6379
port 6379
# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for connections.
#
# bind 127.0.0.1
# Close the connection after a client is idle for N seconds (0 to disable)
timeout 300
# Set server verbosity to 'debug'
# it can be one of:
# debug (a lot of information, useful for development/testing)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel debug
# Specify the log file name. Also 'stdout' can be used to force
# the demon to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile stdout
# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
databases 16
################################ SNAPSHOTTING #################################
#
# Save the DB on disk:
#
# save <seconds> <changes>
#
# Will save the DB if both the given number of seconds and the given
# number of write operations against the DB occurred.
#
# In the example below the behaviour will be to save:
# after 900 sec (15 min) if at least 1 key changed
# after 300 sec (5 min) if at least 10 keys changed
# after 60 sec if at least 10000 keys changed
save 900 1
save 300 10
save 60 10000
# Compress string objects using LZF when dump .rdb databases?
# For default that's set to 'yes' as it's almost always a win.
# If you want to save some CPU in the saving child set it to 'no' but
# the dataset will likely be bigger if you have compressible values or keys.
rdbcompression yes
# The filename where to dump the DB
dbfilename dump.rdb
# For default save/load DB in/from the working directory
# Note that you must specify a directory not a file name.
dir ./
################################# REPLICATION #################################
# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. Note that the configuration is local to the slave
# so for example it is possible to configure the slave to save the DB with a
# different interval, or to listen to another port, and so on.
#
# slaveof <masterip> <masterport>
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
# masterauth <master-password>
################################## SECURITY ###################################
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands. This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# requirepass foobared
################################### LIMITS ####################################
# Set the max number of connected clients at the same time. By default there
# is no limit, and it's up to the number of file descriptors the Redis process
# is able to open. The special value '0' means no limts.
# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
#
# maxclients 128
# Don't use more memory than the specified amount of bytes.
# When the memory limit is reached Redis will try to remove keys with an
# EXPIRE set. It will try to start freeing keys that are going to expire
# in little time and preserve keys with a longer time to live.
# Redis will also try to remove objects from free lists if possible.
#
# If all this fails, Redis will start to reply with errors to commands
# that will use more memory, like SET, LPUSH, and so on, and will continue
# to reply to most read-only commands like GET.
#
# WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
# 'state' server or cache, not as a real DB. When Redis is used as a real
# database the memory usage will grow over the weeks, it will be obvious if
# it is going to use too much memory in the long run, and you'll have the time
# to upgrade. With maxmemory after the limit is reached you'll start to get
# errors for write operations, and this may even lead to DB inconsistency.
#
# maxmemory <bytes>
############################## APPEND ONLY MODE ###############################
# By default Redis asynchronously dumps the dataset on disk. If you can live
# with the idea that the latest records will be lost if something like a crash
# happens this is the preferred way to run Redis. If instead you care a lot
# about your data and don't want to that a single record can get lost you should
# enable the append only mode: when this mode is enabled Redis will append
# every write operation received in the file appendonly.log. This file will
# be read on startup in order to rebuild the full dataset in memory.
#
# Note that you can have both the async dumps and the append only file if you
# like (you have to comment the "save" statements above to disable the dumps).
# Still if append only mode is enabled Redis will load the data from the
# log file at startup ignoring the dump.rdb file.
#
# The name of the append only file is "appendonly.log"
#
# IMPORTANT: Check the BGREWRITEAOF to check how to rewrite the append
# log file in background when it gets too big.
appendonly no
# The fsync() call tells the Operating System to actually write data on disk
# instead to wait for more data in the output buffer. Some OS will really flush
# data on disk, some other OS will just try to do it ASAP.
#
# Redis supports three different modes:
#
# no: don't fsync, just let the OS flush the data when it wants. Faster.
# always: fsync after every write to the append only log . Slow, Safest.
# everysec: fsync only if one second passed since the last fsync. Compromise.
#
# The default is "always" that's the safer of the options. It's up to you to
# understand if you can relax this to "everysec" that will fsync every second
# or to "no" that will let the operating system flush the output buffer when
# it want, for better performances (but if you can live with the idea of
# some data loss consider the default persistence mode that's snapshotting).
appendfsync always
# appendfsync everysec
# appendfsync no
############################### ADVANCED CONFIG ###############################
# Glue small output buffers together in order to send small replies in a
# single TCP packet. Uses a bit more CPU but most of the times it is a win
# in terms of number of queries per second. Use 'yes' if unsure.
glueoutputbuf yes
# Use object sharing. Can save a lot of memory if you have many common
# string in your dataset, but performs lookups against the shared objects
# pool so it uses more CPU and can be a bit slower. Usually it's a good
# idea.
#
# When object sharing is enabled (shareobjects yes) you can use
# shareobjectspoolsize to control the size of the pool used in order to try
# object sharing. A bigger pool size will lead to better sharing capabilities.
# In general you want this value to be at least the double of the number of
# very common strings you have in your dataset.
#
# WARNING: object sharing is experimental, don't enable this feature
# in production before of Redis 1.0-stable. Still please try this feature in
# your development environment so that we can test it better.
# shareobjects no
# shareobjectspoolsize 1024
3:在cmd下面执行以下命令,指定它使用我们的redis.conf,同时也是启动,把redis运行起来,这里指定用redis.conf的配置运行服务器
D:\redis-2.0.2>redis-server.exe redis.conf
4:开一新DOS窗口cmd.执行以下命令,这是Redis的客户端程序:
redis-cli.exe -h 172.18.5.1 -p 6379
172.18.5.1是我本机IP地址,端口6379就是上面配置文件中指定的监听端口
执行完成后,应该能看到redis启动了,这时在第一个cmd窗口可以看到连接信息。
执行一条保存key value操作
set mystock 300156
再查询一下
get mystock
=================================
可以完全参考以下地址的资料:
http://hi.baidu.com/zchare/blog/item/bd6034f325d1c65f352acca1.html(very good,安全可以成功)
http://cardyn.iteye.com/blog/794194
http://zhaohaolin.iteye.com/blog/1017561
二、redis配置选项:
指定redis的配置文件,如没有指定,则使用默认设置
D:\redis-2.0.0-rc2>redis-server.exe redis.conf
redis.conf配置选项如下
daemonize 是否以后台进程运行,默认为no
pidfile 如以后台进程运行,则需指定一个pid,默认为/var/run/redis.pid
bind 绑定主机IP,默认值为127.0.0.1(注释)
port 监听端口,默认为6379
timeout 超时时间,默认为300(秒)
loglevel 日志记录等级,有4个可选值,debug,verbose(默认值),notice,warning
logfile 日志记录方式,默认值为stdout
databases 可用数据库数,默认值为16,默认数据库为0
save <seconds> <changes> 指出在多长时间内,有多少次更新操作,就将数据同步到数据文件。这个可以多个条件配合,比如默认配置文件中的设置,就设置了三个条件。
save 900 1 900秒(15分钟)内至少有1个key被改变
save 300 10 300秒(5分钟)内至少有300个key被改变
save 60 10000 60秒内至少有10000个key被改变
rdbcompression 存储至本地数据库时是否压缩数据,默认为yes
dbfilename 本地数据库文件名,默认值为dump.rdb
dir 本地数据库存放路径,默认值为 ./
slaveof <masterip> <masterport> 当本机为从服务时,设置主服务的IP及端口(注释)
masterauth <master-password> 当本机为从服务时,设置主服务的连接密码(注释)
requirepass 连接密码(注释)
maxclients 最大客户端连接数,默认不限制(注释)
maxmemory <bytes> 设置最大内存,达到最大内存设置后,Redis会先尝试清除已到期或即将到期的Key,当此方法处理后,任到达最大内存设置,将无法再进行写入操作。(注释)
appendonly 是否在每次更新操作后进行日志记录,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为redis本身同步数据文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认值为no
appendfilename 更新日志文件名,默认值为appendonly.aof(注释)
appendfsync 更新日志条件,共有3个可选值。no表示等操作系统进行数据缓存同步到磁盘,always表示每次更新操作后手动调用fsync()将数据写到磁盘,everysec表示每秒同步一次(默认值)。
vm-enabled 是否使用虚拟内存,默认值为no
vm-swap-file 虚拟内存文件路径,默认值为/tmp/redis.swap,不可多个Redis实例共享
vm-max-memory 将所有大于vm-max-memory的数据存入虚拟内存,无论vm-max-memory设置多小,所有索引数据都是内存存储的(Redis的索引数据就是keys),也就是说,当vm-max-memory设置为0的时候,其实是所有value都存在于磁盘。默认值为0。
Redis官方文档对VM的使用提出了一些建议:
当你的key很小而value很大时,使用VM的效果会比较好.因为这样节约的内存比较大.
当你的key不小时,可以考虑使用一些非常方法将很大的key变成很大的value,比如你可以考虑将key,value组合成一个新的value.
最好使用linux ext3 等对稀疏文件支持比较好的文件系统保存你的swap文件.
vm-max-threads这个参数,可以设置访问swap文件的线程数,设置最好不要超过机器的核数.如果设置为0,那么所有对swap文件的操作都是串行的.可能会造成比较长时间的延迟,但是对数据完整性有很好的保证.redis-cli.exe:命令行客户端,测试用
D:\redis-2.0.0-rc2>redis-cli.exe -h 127.0.0.1 -p 6379
三、redis java操作(包括list/Map/删除/新增/覆盖/排序等都支持)
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import javax.sound.midi.Soundbank;
import java.util.*;
/**
* @author: flychao88
* Time: 2012.5.7 16:23:15
*/
public class RedisTest {
JedisPool pool;
Jedis jedis;
@Before
public void setUp() {
pool = new JedisPool(new JedisPoolConfig(), "172.16.100.184");
jedis = pool.getResource();
jedis.auth("password");
}
/**
* Redis存储初级的字符串
* CRUD
*/
@Test
public void testBasicString(){
//-----添加数据----------
jedis.set("name","minxr");//向key-->name中放入了value-->minxr
System.out.println(jedis.get("name"));//执行结果:minxr
//-----修改数据-----------
//1、在原来基础上修改
jedis.append("name","jarorwar"); //很直观,类似map 将jarorwar append到已经有的value之后
System.out.println(jedis.get("name"));//执行结果:minxrjarorwar
//2、直接覆盖原来的数据
jedis.set("name","闵晓荣");
System.out.println(jedis.get("name"));//执行结果:闵晓荣
//删除key对应的记录
jedis.del("name");
System.out.println(jedis.get("name"));//执行结果:null
/**
* mset相当于
* jedis.set("name","minxr");
* jedis.set("jarorwar","闵晓荣");
*/
jedis.mset("name","minxr","jarorwar","闵晓荣");
System.out.println(jedis.mget("name","jarorwar"));
}
/**
* jedis操作Map
*/
@Test
public void testMap(){
Map<String,String> user=new HashMap<String,String>();
user.put("name","minxr");
user.put("pwd","password");
jedis.hmset("user",user);
//取出user中的name,执行结果:[minxr]-->注意结果是一个泛型的List
//第一个参数是存入redis中map对象的key,后面跟的是放入map中的对象的key,后面的key可以跟多个,是可变参数
List<String> rsmap = jedis.hmget("user", "name");
System.out.println(rsmap);
//删除map中的某个键值
// jedis.hdel("user","pwd");
System.out.println(jedis.hmget("user", "pwd")); //因为删除了,所以返回的是null
System.out.println(jedis.hlen("user")); //返回key为user的键中存放的值的个数1
System.out.println(jedis.exists("user"));//是否存在key为user的记录 返回true
System.out.println(jedis.hkeys("user"));//返回map对象中的所有key [pwd, name]
System.out.println(jedis.hvals("user"));//返回map对象中的所有value [minxr, password]
Iterator<String> iter=jedis.hkeys("user").iterator();
while (iter.hasNext()){
String key = iter.next();
System.out.println(key+":"+jedis.hmget("user",key));
}
}
/**
* jedis操作List
*/
@Test
public void testList(){
//开始前,先移除所有的内容
jedis.del("java framework");
System.out.println(jedis.lrange("java framework",0,-1));
//先向key java framework中存放三条数据
jedis.lpush("java framework","spring");
jedis.lpush("java framework","struts");
jedis.lpush("java framework","hibernate");
//再取出所有数据jedis.lrange是按范围取出,
// 第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有
System.out.println(jedis.lrange("java framework",0,-1));
}
/**
* jedis操作Set
*/
@Test
public void testSet(){
//添加
jedis.sadd("sname","minxr");
jedis.sadd("sname","jarorwar");
jedis.sadd("sname","闵晓荣");
jedis.sadd("sanme","noname");
//移除noname
jedis.srem("sname","noname");
System.out.println(jedis.smembers("sname"));//获取所有加入的value
System.out.println(jedis.sismember("sname", "minxr"));//判断 minxr 是否是sname集合的元素
System.out.println(jedis.srandmember("sname"));
System.out.println(jedis.scard("sname"));//返回集合的元素个数
}
@Test
public void test() throws InterruptedException {
//keys中传入的可以用通配符
System.out.println(jedis.keys("*")); //返回当前库中所有的key [sose, sanme, name, jarorwar, foo, sname, java framework, user, braand]
System.out.println(jedis.keys("*name"));//返回的sname [sname, name]
System.out.println(jedis.del("sanmdde"));//删除key为sanmdde的对象 删除成功返回1 删除失败(或者不存在)返回 0
System.out.println(jedis.ttl("sname"));//返回给定key的有效时间,如果是-1则表示永远有效
jedis.setex("timekey", 10, "min");//通过此方法,可以指定key的存活(有效时间) 时间为秒
Thread.sleep(5000);//睡眠5秒后,剩余时间将为<=5
System.out.println(jedis.ttl("timekey")); //输出结果为5
jedis.setex("timekey", 1, "min"); //设为1后,下面再看剩余时间就是1了
System.out.println(jedis.ttl("timekey")); //输出结果为1
System.out.println(jedis.exists("key"));//检查key是否存在
System.out.println(jedis.rename("timekey","time"));
System.out.println(jedis.get("timekey"));//因为移除,返回为null
System.out.println(jedis.get("time")); //因为将timekey 重命名为time 所以可以取得值 min
//jedis 排序
//注意,此处的rpush和lpush是List的操作。是一个双向链表(但从表现来看的)
jedis.del("a");//先清除数据,再加入数据进行测试
jedis.rpush("a", "1");
jedis.lpush("a","6");
jedis.lpush("a","3");
jedis.lpush("a","9");
System.out.println(jedis.lrange("a",0,-1));// [9, 3, 6, 1]
System.out.println(jedis.sort("a")); //[1, 3, 6, 9] //输入排序后结果
System.out.println(jedis.lrange("a",0,-1));
}
六、redis和memcached比较
1、memcached多线程 redis单线程
2、memcached支持简单的key/value redis支持最为常用的数据类型主要由五种:String、Hash、List、Set和Sorted Set(最吸引人的就此,而且可以对list排序等)
3、memcached客户端实现分布式 redis服务器端实现分布式
4、
- redis-2.0.2.zip (1.1 MB)
- 下载次数: 19
- redis.conf.rar (3.4 KB)
- 下载次数: 17
- RedisTest.rar (1.7 KB)
- 下载次数: 18
- redisUtils(考虑了回收等问题,生产环境可用).rar (1.5 MB)
- 下载次数: 18
- Redis发布订阅模式demo.rar (17.3 KB)
- 下载次数: 12
- redis(2.6.14).conf.rar (13.1 KB)
- 下载次数: 9
- redis-windows启动脚本.rar (424 Bytes)
- 下载次数: 11
- redis2.6.12_win.zip (1.1 MB)
- 下载次数: 7
- redis2.6.14_其他人的经验-积累了一些经验及配置文件中文详解_.rar (1.4 MB)
- 下载次数: 19
- Redis桌面管理工具V1.5官方最新版_868291.rar (3 MB)
- 下载次数: 41
- RedisClient-master-监控工具GUI2.0.rar (4.4 MB)
- 下载次数: 24
发表评论
-
高可用、开源的Redis缓存集群方案
2015-04-16 12:25 3761推荐综合架构交流群:J ... -
各种Nosql数据库系统对比及应用场景分析
2015-04-15 16:29 762关键字:各种Nosql数据库系统对比及应用场景分析 导读:Kr ... -
MongoDB中缩减Shard集群(删除一个Shard)--删除一个分片
2015-03-13 12:29 3649关键字:MongoDB中缩减Shard集群(删除一个Shard ... -
redis中文API
2014-11-11 18:06 2308关键字:redis中文API RED ... -
Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍
2014-05-29 13:00 7960关键字:Redis的Java客户端Jedis的八种调用方式(事 ... -
redis应用场景及产品定位
2014-05-23 11:50 5926关键字:redis应用场景及产品定位 总结与memcach ... -
memcached(windows)安装与配置
2011-07-29 11:47 24297关键字:memcached(windows ...
相关推荐
"redis for windows_3.0"是Redis官方针对Windows平台发布的版本,它确保了Redis在Windows操作系统上的稳定运行,同时也保持了与Linux版本类似的性能。 1. 安装与配置: - Redis for Windows通常以zip文件形式提供...
总之,这个“redis for windows”压缩包为Windows用户提供了方便的Redis环境,通过理解并掌握其中的文件及配置,你可以在Windows平台上充分利用Redis的功能。无论是进行开发工作,还是在生产环境中部署,这些资源都...
redis 5.0.14.1 资源打包,方便小伙伴们下载。 压缩文件包含 1.Redis-x64-5.0.14.1.msi 2.Redis-x64-5.0.14.1.zip 3.Source code (zip) 4.Source code (tar.gz)
- 下载:从官方或第三方源获取"redis for windows 6.0.9.zip",解压至合适的位置。 - 启动服务:进入解压后的“bin”目录,运行`redis-server.exe`启动Redis服务器。默认配置文件为`redis.windows.conf`,可以按需...
**Redis for Windows 64 知识点详解** Redis 是一个高性能的键值数据库,它在内存中存储数据,提供高速的数据读写能力。通常 Redis 用于缓存、消息队列、分布式计数器等场景。然而,Redis 原生支持的是 Unix-like ...
"redis64 for windows"是Redis官方为Windows操作系统提供的64位版本,使得在Windows环境下也能便捷地使用Redis服务。 1. Redis的基本概念: - 键值对:Redis的核心数据模型是键值对(Key-Value),键和值都可以是...
以下是对"Redis for Windows"的详细解读: 1. Redis简介: Redis是一个基于内存的数据结构存储系统,它支持多种数据类型,如字符串、哈希、列表、集合、有序集合等。这些数据结构使得Redis在处理各种场景时非常...
**Redis for Windows 工具安装与配置指南** Redis是一款高性能的键值存储系统,常用于数据库、缓存和消息中间件。本指南将详细介绍如何在Windows操作系统上安装和配置Redis,以及如何使用可视化管理工具。 ### 1. ...
**php_redis for Windows** 在Windows环境下开发和运行PHP应用程序时,常常需要与Redis这种高性能的键值存储系统进行交互。`php_redis`扩展是PHP语言与Redis通信的关键桥梁,它使得PHP开发者能够充分利用Redis的...
**Redis 7.0.0 for Windows:新一代内存数据存储** Redis,全称为Remote Dictionary Server,是一款开源的、高性能的键值对存储系统,广泛应用于缓存、数据库、消息中间件等多个领域。随着Redis 7.0.0的发布,...
Redis for Windows 2.6.13是专为Windows操作系统设计的一个稳定版本,允许Windows用户利用Redis的强大功能。这个版本是针对Windows 32位和64位系统的,确保在各种Windows环境下都能正常运行。 在Redis 2.6.13中,有...
其中最常用的是redis-windows,它是一个Windows下的Redis管理工具,可以方便地安装、配置和管理Redis服务器。 redis-windows提供了一个简单易用的界面,可以让用户轻松地启动、停止和重启Redis服务器,以及管理...
windows环境安装redis的zip包,已经做过测试,需要解压的可以使用7z进行压缩使用!
总的来说,"最新redis for windows"为Windows用户提供了一个便捷的Redis环境,使得开发者能够在Windows平台充分利用Redis的强大功能。但需要注意的是,尽管如此,Unix-like系统仍然是Redis的最佳运行环境,因为它们...