设为首页 加入收藏

TOP

Java使用Redis初探(一)
2015-11-21 01:50:44 来源: 作者: 【 】 浏览:2
Tags:Java 使用 Redis 初探

Redis的相关概念不做介绍了,大家也可以先了解下Memcached,然后比较下二者的区别,就会有个整体的印象。

服务器端通常选择Linux , Redis对于linux是官方支持的,使用资料很多,需要下载相关服务器端程序 ,然后解压安装。因为能力和条件有限,我只简单介绍下windows上如何安装和使用,有兴趣的可以娱乐一下。

服务器端程序下载地址:https://github.com/ServiceStack/redis-windows.git

如果不好操作的话到这来:http://download.csdn.net/detail/u013283727/8212831

下载完后使用cmd进入下载文件的目录中,尝试以下操作:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\>cd redis64-latest

C:\redis64-latest>redis-server redis.windows.conf --maxmemory 200m
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 2.8.17 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in stand alone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 4552
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

[4552] 01 Dec 13:38:53.147 # Server started, Redis version 2.8.17
[4552] 01 Dec 13:38:53.147 * DB loaded from disk: 0.000 seconds
[4552] 01 Dec 13:38:53.147 * The server is now ready to accept connections on po
rt 6379


客户端使用java程序来连接,在这里介绍两种常用的方法

(Jar包直接找maven要:http://www.mvnrepository.com 一搜就出来了)

1.Redisson

/**
 * @author fcs
 * Redisson Example
 */
public class RedissonTest {
	public static void main(String[] args) {
		//1.初始化
		Config config = new Config();
		config.setConnectionPoolSize(10);
		config.addAddress("127.0.0.1:6379");
		Redisson redisson = Redisson.create(config);
		System.out.println("redis连接接成功。。。。。");
		
		//2.测试concurrentMap,put时候就会同步到redis中
		ConcurrentMap map = redisson.getMap("firstMap");
		map.put("changshengfeng", "男");
		map.put("yongtaoliu", "男");
		map.put("qiaozhu", "女");
		
		ConcurrentMap resultMap = redisson.getMap("firstMap");
		System.out.println("resultMap == "+resultMap.keySet());
		//关闭连接
		redisson.shutdown();
	}
}

2.Jedis

/**
 * @author fcs
 * test about jedis
 * Dec 1, 2014
 */
public class JedisTest {
	private static Jedis jedis;
	
	@Before
	public void setup(){
		jedis = new Jedis("127.0.0.1", 6379);
		System.out.println("Redis服务器已连接....");
//		jedis.auth("admin");   //权限验证
	}
	
	/**
	 * redis 存储字符串
	 */
	@Test
	public void testString(){
		//添加数据
		jedis.set("name", "fcs");
		System.out.println(jedis.get("name"));//获取结果
		
		jedis.append("name", "is handsome");//拼接
		
		jedis.del("name");//删除某个键
		System.out.println(jedis.get("name"));
		
		jedis.mset("name","changsheng","age","22","qq","646653132");//设置多个键值对
		jedis.incr("age");//加1操作   在投票中可能用的上
		System.out.println(jedis.get("name")+"--"+jedis.get("age")+"--"+jedis.get("qq"));
	}
	
	/**
	 * 操作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  第二个是其实位置  第三个是结束位置
		System.out.println(jedis.lrange("java framework", 0, -1));
		
		jedis.del("java framework");
		jedis.rpush("java framework", "spring");
		jedis.rpush("java framework
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇foreingkey约束 下一篇关于存储过程可空条件查询

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: