Redis服务搭建和使用

来源:互联网 发布:linux查看文件命令大小 编辑:程序博客网 时间:2024/06/08 13:24

Redis是什么?
Redis – 缓存数据的工具之一

Redis服务的搭建:
Redis在window平台安装:
1.到https://github.com/dmajkic/redis/downloads下载对应的redis安装包
2.解压后,包含文件:
redis-server.exe:服务程序
redis.conf:配置文件,可以修改,一般默认就ok了
redis-check-dump.exe:本地数据库检查
redis-check-aof.exe:更新日志检查
redis-benchmark.exe:性能测试,用以模拟同时由N个客户端发送M个 SETs/GETs 查询 (类似于 Apache 的ab 工具).
3.配置环境变量

Redis在Linux平台上安装:
1. 到官方网站:http://www.redis.io/download下载对应安装包
2. 解压
3. 进入解压文件下,编译,命令执行如下

$ wget http://download.redis.io/releases/redis-2.8.13.tar.gz$ tar xzf redis-2.8.13.tar.gz$ cd redis-2.8.13$ make

Redis服务启动:

redis-server redis.conf

Redis连接

1:   ./redis-cli -h (ip) -p(port)2:   telnet 127.0.0.1 6379       (ip接端口)

python 使用Redis模块
python redis具体使用可以参考:http://www.cnblogs.com/wangtp/p/5636872.html

一个简单的例子

import sysimport redisimport concurrent.futuresimport ConfigParserconfig = ConfigParser.ConfigParser()config.readfp(open('config/config.ini'), 'rb')HOST = '%s' % config.get('redis', 'ip')PORT = config.get('redis', 'port')r = redis.Redis(host=HOST, port=PORT)fred = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]def check_server():    try:        print r.info()    except redis.exceptions.ConnectionError:        print >> sys.stderr, "Error: cannot connect to redis server. Is the server running?"        sys.exit(1)def f(x):    res = x * x    r.rpush("test", res)    # r.delete("test")def main():    # with 保证所有线程都执行完,再执行下面操作    with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:        for num in fred:            executor.submit(f, num)    #    print r.lrange("test", 0, -1)####################if __name__ == "__main__":    check_server()    ###    r.delete("test")    main()

Jedis客户端使用
获取
Jedis的github地址为: https://github.com/xetorthio/jedis
可以通过下面两种方式来获取Jedis的Jar包

直接下载Jar包
https://github.com/xetorthio/jedis/releases

从Maven仓库中取Jar包

<dependency>        <groupId>redis.clients</groupId>        <artifactId>jedis</artifactId>        <version>2.5.2</version>        <type>jar</type>        <scope>compile</scope>    </dependency>

使用
最简单的使用方式

Jedis jedis = new Jedis("localhost");jedis.set("foo", "bar");String value = jedis.get("foo");

当然,这种方式并不好,因为每次使用都要新建立一个连接,而且Jedis并不是线程安全的,在并发访问的情况下容易出奇怪的问题。所以应该使用下面的这种方式:使用池 来做。

JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");Jedis jedis = pool.getResource();try {    /// 开始使用    jedis.set("foo", "bar");    String foobar = jedis.get("foo");    jedis.zadd("sose", 0, "car");     jedis.zadd("sose", 0, "bike");     Set<String> sose = jedis.zrange("sose", 0, -1);} finally {    /// 使用完后,将连接放回连接池    if (null != jedis) {        jedis.close();    }}/// 应用退出时,关闭连接池:pool.destroy();这种使用池的方式一般都能满足我们的要求,但是有时候我们使用多台Redis服务器时,我们需要将不同的key放到不同的Redis服务器上面,这时我们可以根据业务的不同来选择不同的Redis服务器。这么做可以一定程度的解决问题,但是还会有另外的问题生产,如:我们不容易确定哪个业务产品的数据有多大。这样会使数据不能平均的分配到多台Redis服务器上面。 这时我们需要使用分片的技术。代码如下:// 分片信息  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();JedisShardInfo si = new JedisShardInfo("localhost", 6379);si.setPassword("foobared");shards.add(si);si = new JedisShardInfo("localhost", 6380);si.setPassword("foobared");shards.add(si);// 池对象  ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards);// 开始使用  ShardedJedis jedis = pool.getResource();jedis.set("a", "foo");.... // do your work herepool.returnResource(jedis);.... // a few moments laterShardedJedis jedis2 = pool.getResource();jedis.set("z", "bar");pool.returnResource(jedis);pool.destroy();

集群

Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();//Jedis Cluster will attempt to discover cluster nodes automaticallyjedisClusterNodes.add(new HostAndPort("127.0.0.1", 7379));JedisCluster jc = new JedisCluster(jedisClusterNodes);jc.set("foo", "bar");String value = jc.get("foo");

来自:http://my.oschina.net/gccr/blog/307725

1 0
原创粉丝点击