<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);
}
}