1.window下使用
下载地址:http://pan.baidu.com/s/1gdCoKyn
然后运行redis-server.exe
启动好服务如下,
2.打开cmd窗口,cd到目录C:\Users\Administrator\Desktop\redis-2.4.5-win32-win64\32bit
执行:redis-cli.exe(客户端)
3.redis命令-数据存储介绍
http://redis.io/topics/data-types-intro
官网的例子最好照着做一遍,能快速学会redis,除了最后Bitmap、lexicograpical这些比较难懂,而且也不见得常用到,而外基本都是比较通俗易懂的
4.Java访问redis-jedius
jedis-2.7.2.jar下载地址:http://pan.baidu.com/s/1o6mXYIm
给出一个jedius-api:http://tool.oschina.net/uploads/apidocs/
再给一个Java封装类:
import java.util.ArrayList; import java.util.List; import java.util.Set; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; public class RedisTool { private static String redisHost = "127.0.0.1"; private static JedisPool pool = new JedisPool(redisHost); // get命令,数据库默认选择序号为0 public static Object get(String key) { Jedis jedis = null; try { jedis = pool.getResource(); byte[] data = jedis.get(key.getBytes()); if (data == null || data.length <= 0) { return null; } return SerializeUtil.unserialize(data); } catch (Exception e) { throw new RuntimeException("Redis出现错误!", e); } finally { close(jedis); } } // get命令,第二个参数为选择数据库 public static Object get(String key, int dbIndex) { Jedis jedis = null; try { jedis = pool.getResource(); jedis.select(dbIndex); byte[] data = jedis.get(key.getBytes()); if (data == null || data.length <= 0) { return null; } return SerializeUtil.unserialize(data); } catch (Exception e) { throw new RuntimeException("Redis出现错误!", e); } finally { close(jedis); } } // set命令,默认选择序号为0 public static void set(String key, Object value) { Jedis jedis = null; try { jedis = pool.getResource(); jedis.set(key.getBytes(), SerializeUtil.serialize(value)); } catch (Exception e) { throw new RuntimeException("Redis出现错误!", e); } finally { close(jedis); } } // set命令,第二个参数为选择数据库 public static void set(String key, Object value, int dbIndex) { Jedis jedis = null; try { jedis = pool.getResource(); jedis.select(dbIndex); jedis.set(key.getBytes(), SerializeUtil.serialize(value)); } catch (Exception e) { throw new RuntimeException("Redis出现错误!", e); } finally { close(jedis); } } // hset命令,就是普通的Java-Map对象形式,key标识不同的Map对象,field为指定Map对象的键值 public static void hset(String key, String field, Object value) { Jedis jedis = null; try { jedis = pool.getResource(); jedis.hset(key.getBytes(), field.getBytes(), SerializeUtil.serialize(value)); } catch (Exception e) { throw new RuntimeException("Redis出现错误!", e); } finally { close(jedis); } } // hget,就是获取指定key值的Map对象的field字段值 public static Object hget(String key, String field) { Jedis jedis = null; try { jedis = pool.getResource(); byte[] data = jedis.hget(key.getBytes(), field.getBytes()); if (data == null || data.length <= 0) { return null; } return SerializeUtil.unserialize(data); } catch (Exception e) { throw new RuntimeException("Redis出现错误!", e); } finally { close(jedis); } } // remove为移除一条记录 public static void remove(String key) { Jedis jedis = null; try { jedis = pool.getResource(); jedis.del(key.getBytes()); } catch (Exception e) { throw new RuntimeException("Redis出现错误!", e); } finally { close(jedis); } } // redis实现的自动+1序列获取 public static long getAutoId(String key) { Jedis jedis = null; long id = 1; try { jedis = pool.getResource(); id = jedis.incr(key.getBytes()); } catch (Exception e) { throw new RuntimeException("Redis出现错误!", e); } finally { close(jedis); } return id; } // list-push,key标识某list对象,而且是left-push public static void lpush(String key, Object value) { Jedis jedis = null; try { jedis = pool.getResource(); jedis.lpush(key.getBytes(), SerializeUtil.serialize(value)); } catch (Exception e) { throw new RuntimeException("Redis出现错误!", e); } finally { close(jedis); } } // list-push,key标识某list对象,而且是right-push public static void rpush(String key, Object value) { Jedis jedis = null; try { jedis = pool.getResource(); jedis.rpush(key.getBytes(), SerializeUtil.serialize(value)); } catch (Exception e) { throw new RuntimeException("Redis出现错误!", e); } finally { close(jedis); } } // 获取指定key值的list对象的所有元素 public static List<Object> lrange(String key) { Jedis jedis = null; List<Object> result = null; try { jedis = pool.getResource(); List<byte[]> list = jedis.lrange(key.getBytes(), 0, -1); result = new ArrayList<Object>(list.size()); for (byte[] o : list) { result.add(SerializeUtil.unserialize(o)); } } catch (Exception e) { throw new RuntimeException("Redis出现错误!", e); } finally { close(jedis); } return result; } // left-pop一个指定key值的list对象 public static Object lpop(String key) { Jedis jedis = null; Object object = null; try { jedis = pool.getResource(); byte[] bs = jedis.lpop(key.getBytes()); object = SerializeUtil.unserialize(bs); } catch (Exception e) { throw new RuntimeException("Redis出现错误!", e); } finally { close(jedis); } return object; } // right-pop一个指定key值的list对象 public static Object rpop(String key) { Jedis jedis = null; Object object = null; try { jedis = pool.getResource(); byte[] bs = jedis.rpop(key.getBytes()); object = SerializeUtil.unserialize(bs); } catch (Exception e) { throw new RuntimeException("Redis出现错误!", e); } finally { close(jedis); } return object; } // list-index,查找指定key值的list对象的第index号元素 public static Object lindex(String key, int index) { Jedis jedis = null; Object object = null; try { jedis = pool.getResource(); byte[] bs = jedis.lindex(key.getBytes(), index); object = SerializeUtil.unserialize(bs); } catch (Exception e) { throw new RuntimeException("Redis出现错误!", e); } finally { close(jedis); } return object; } // 关闭jedis private static void close(Jedis jedis) { if (jedis != null) { jedis.close(); } } // list-length,获取指定key值的list对象的元素个数 public static long llen(String key) { Jedis jedis = null; long object; try { jedis = pool.getResource(); object = jedis.llen(key.getBytes()); } catch (Exception e) { throw new RuntimeException("Redis出现错误!", e); } finally { if (jedis != null) { jedis.close(); } } return object; } // 获取当前数据库(默认序号为0)的所有keys,相当于“keys *”命令 public static List<String> keys() { Jedis jedis = null; List<String> list = null; try { jedis = pool.getResource(); Set<byte[]> set = jedis.keys("*".getBytes()); list = new ArrayList<String>(set.size()); for (byte[] bs : set) { list.add(new String(bs, "UTF8")); } } catch (Exception e) { throw new RuntimeException("Redis出现错误!", e); } finally { if (jedis != null) { jedis.close(); } } return list; } }
5.linux下安装redis
安装包为redis-3.0.4.tar.gz,给出下载地址:http://pan.baidu.com/s/1jGm0ncy
先cd到/usr/local/,然后mkdir一个文件夹redis,之后把redis-3.0.4.tar.gz拷贝到/usr/local/redis
解压,执行tar -cxvf redis-3.0.4.tar.gz
之后cd到redis-3.0.4,执行make命令
然后执行make install命令
完成以后redis服务自动启动,这次我的redis服务是部署在210.10.5.189上面,本机ip为210.10.5.102
打开cmd窗口,cd到有redis-cli.exe的那个目录下,C:\Users\Administrator\Desktop\redis-2.4.5-win32-win64\32bit,然后执行redis-cli.exe -h 210.10.5.189 -p 6379,如下
跟windows上部署的没什么区别了。
6.还有几个补充说明
6.1命令
flushdb--删除当前数据库中的所有Key flushall--删除所有数据库中的key
6.2 linux-redis查看启动进程:
ps -ef | grep redis
6.3 配置redis最大分配内存
vim redis.conf
修改maxmemory <bytes>为maxmemory <2147483648>
为防止内存全部被redis耗光,达到最大内存设置后,Redis会先尝试清除已到期或即将到期的Key,也会从free-list里清除一些key-value。当以上方法都没有效果,那redis就不再支持写入操作(如set,lpush),但不影响读操作如get。(maxmemory配置默认是注释掉的,没限制,跟RAM相同)