redis 高速缓存框架

<p style="padding-top: 0px; padding-bottom: 15px; margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-family: "Microsoft Yahei"; font-size: 16px; text-align: justify; line-height: 30px;"><strong>引用:<a target=_blank href="http://os.51cto.com/art/201403/431103.htm">http://os.51cto.com/art/201403/431103.htm</a></strong></p><p style="padding-top: 0px; padding-bottom: 15px; margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-family: "Microsoft Yahei"; font-size: 16px; text-align: justify; line-height: 30px;"><strong>
</strong></p><p style="padding-top: 0px; padding-bottom: 15px; margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-family: "Microsoft Yahei"; font-size: 16px; text-align: justify; line-height: 30px;"><strong>安装Redis</strong></p><p style="padding-top: 0px; padding-bottom: 15px; margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-family: "Microsoft Yahei"; font-size: 16px; text-align: justify; line-height: 30px;">官方网站:<a target=_blank href="http://redis.io/" rel="nofollow" style="color: rgb(255, 66, 0); transition: all 0.5s ease; text-decoration: none;">http://redis.io/</a></p><p style="padding-top: 0px; padding-bottom: 15px; margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-family: "Microsoft Yahei"; font-size: 16px; text-align: justify; line-height: 30px;">官方下载:<a target=_blank href="http://redis.io/download" rel="nofollow" style="color: rgb(255, 66, 0); transition: all 0.5s ease; text-decoration: none;">http://redis.io/download</a> 可以根据需要下载不同版本</p><p style="padding-top: 0px; padding-bottom: 15px; margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-family: "Microsoft Yahei"; font-size: 16px; text-align: justify; line-height: 30px;">windows版:<a target=_blank href="https://github.com/mythz/redis-windows" rel="nofollow" style="color: rgb(255, 66, 0); transition: all 0.5s ease; text-decoration: none;">https://github.com/mythz/redis-windows</a></p><p style="padding-top: 0px; padding-bottom: 15px; margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-family: "Microsoft Yahei"; font-size: 16px; text-align: justify; line-height: 30px;">进入redis目录后 开启服务  redis-server.exe启动服务</p><p style="padding-top: 0px; padding-bottom: 15px; margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-family: "Microsoft Yahei"; font-size: 16px; text-align: justify; line-height: 30px;">java需要引用的jar</p><p style="padding-top: 0px; padding-bottom: 15px; margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-family: "Microsoft Yahei"; font-size: 16px; text-align: justify; line-height: 30px;"><pre name="code" class="html"><!--Redis --> 
<dependency> 
<span style="white-space:pre">	</span><groupId>redis.clients</groupId>
<span style="white-space:pre">	</span><artifactId>jedis</artifactId> 
<span style="white-space:pre">	</span><version>2.0.0</version> 
<span style="white-space:pre">	</span><type>jar</type> 
<span style="white-space:pre">	</span><scope>compile</scope> 
</dependency> 

 
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">package redis.com.redis;

import java.util.HashMap;
import java.util.Map;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisTest {
	
	private static final JedisPool pool = new JedisPool(new JedisPoolConfig(), "127.0.0.1");
	
	private static final Jedis jedis = pool.getResource();

	public static void main(String[] args) {
//		new RedisTest().testList();
//		new RedisTest().testSet();
		//将数据保存在硬盘上redis下次启动会自动使用里面的数据
//		String result = jedis.save();
//		System.out.println(result);
		//通过通配符获取所有的key
		System.out.println(jedis.keys("*"));
	}
	
	public void testMap(){
		Map<String, String> map = new HashMap<String, String>();
		map.put("name","lilei");
		map.put("sex", "M");
		map.put("age", "20");
		jedis.hmset("user", map);
		System.out.println(jedis.hmget("user", "name","sex"));
		jedis.hdel("user", "sex");
		System.out.println(jedis.hmget("user", "name","sex"));
		System.out.println(jedis.hlen("user"));
		System.out.println(jedis.exists("user"));
		System.out.println(jedis.hkeys("user"));
		System.out.println(jedis.hvals("user"));
	}
	
	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)); 
	}
	
	public void testSet(){ 
		//添加 
		jedis.sadd("sname","jarorwar"); 
		jedis.sadd("sname","minxr"); 
		jedis.sadd("sname","闵晓荣"); 
		jedis.sadd("sname","noname"); 
		jedis.sadd("sname","noname"); 
		//移除noname 
		System.out.println(jedis.smembers("sname"));//获取所有加入的value 
		jedis.srem("sname","noname"); 
		System.out.println(jedis.sismember("sname", "minxr"));//判断 minxr 是否是sname集合的元素 
		System.out.println(jedis.srandmember("sname")); 
		System.out.println(jedis.srandmember("sname")); 
		System.out.println(jedis.scard("sname"));//返回集合的元素个数 
		} 
	
	public void testSetGet(){
		RedisTest test = new RedisTest();
		String result = test.set("name", "李磊");
		System.out.println(result);
		System.out.println(test.get("name"));
		jedis.append("name", " 男");
		System.out.println(test.get("name"));
		jedis.set("name", "姓名");
		System.out.println(jedis.get("name"));
		
		jedis.mset("name","李磊","sex","男","age","20");
		System.out.println(jedis.mget("name","sex"));
		System.out.println(jedis.mget("name","age"));
		jedis.del("name");
		System.out.println(jedis.get("name"));
		System.out.println(jedis.get("age"));
	}
	
	public String set(String key,String value){
		String result = jedis.set(key, value);
		return result;
	}

	
	public String get(String key){
		return jedis.get(key);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值