Rainie999 发表于 2016-12-19 10:29:14

redis的简单使用和介绍(转载)

  Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类keyvalue存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Python,Ruby,Erlang,PHP,Java客户端,使用很方便。
Redis使用单线程的IO复用模型,自己封装了一个简单的AeEvent事件处理框架,主要实现了epoll、kqueue和select,对于单纯只 有IO操作来说,单线程可以将速度优势发挥到最大,但是Redis也提供了一些简单的计算功能,比如排序、聚合等,对于这些操作,单线程模型实际会严重影 响整体吞吐量,CPU计算过程中,整个IO调度都是被阻塞住的。

  Redis 除了作为存储之外还提供了一些其它方面的功能,比如聚合计算、pubsub、scripting等,对于此类功能需要了解其实现原理,清楚地了解到它的局 限性后,才能正确的使用,比如pubsub功能,这个实际是没有任何持久化支持的,消费方连接闪断或重连之间过来的消息是会全部丢失的,又比如聚合计算和 scripting等功能受Redis单线程模型所限,是不可能达到很高的吞吐量的,需要谨慎使用。

  本例子Linux采用的centOs5.4
  下面来介绍一下redis的安装
view plainprint?


[*]wget http://redis.googlecode.com/files/redis-2.0.4.tar.gz
[*]tar zxvf redis-2.0.4.tar.gz
[*]cd redis-2.0.4
[*]make

  make完后 redis-2.0.4目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli
  安装成功
  

  启动服务
  ./redis-server
  也可以通过启动参数告诉redis使用指定配置文件使用下面命令启动
  ./redis-server redis.conf
  redis.conf是一个默认的配置文件。我们可以根据需要使用自己的配置文件。
  启动redis服务进程后,就可以使用测试客户端程序redis-cli和redis服务交互了
  注意启动的时候,会出现
  WARNING overcommit_memory is set to 0!Background save may fail under
  low memory condition. To fix this issue add'vm.overcommit_memory = 1' to /etc/sysctl.conf and
   10 Aug 20:58:21 * The server is nowready to accept connections on port 6379
   10 Aug 20:58:21 - 0 clientsconnected (0 slaves), 533432 bytes in use
   10 Aug 20:58:30 - 0 clientsconnected (0 slaves), 533432 bytes in use
  由于默认配置是连接到本机的
  这时候你要修改配置文件的ip地址连接你服务器啊
  还有就是执行:sysctl vm.overcommit_memory=1
  然后再启动服务就可以了
  关于redis一些资料的学习可以到http://www.cnblogs.com/xhan/archive/2011/02/08/1949867.html去学习 ,很全面
  下面介绍一个简单java客户端Jedis,大家可以到https://github.com/xetorthio/jedis这网址下载
  这里给大家提供一个简单的对jedis的封装类以供参考
  Redis.java
view plainprint?


[*]package com.ajun.redis;
[*]
[*]import java.util.HashMap;
[*]import java.util.HashSet;
[*]import java.util.List;
[*]import java.util.Map;
[*]import java.util.Set;
[*]
[*]import redis.clients.jedis.Jedis;
[*]import redis.clients.jedis.JedisPool;
[*]import redis.clients.jedis.JedisPoolConfig;
[*]/**
[*]*
[*]* @author ajun
[*]*
[*]*/
[*]public class Redis {
[*]private static JedisPool pool;
[*]private static int DBIndex;
[*]private static String host;
[*]private static int port=6379;
[*]private static int timeout=60*1000;
[*]static {
[*]DBIndex=Integer.parseInt(PubConstant.getConfigureValue("redis_dbindex"));
[*]host=PubConstant.getConfigureValue("redis_host");
[*]
[*]JedisPoolConfig config = new JedisPoolConfig();
[*]config.setMaxActive(100);
[*]config.setMaxIdle(20);
[*]config.setMaxWait((long)1000);
[*]config.setTestOnBorrow(false);
[*]pool = new JedisPool(config, host, port, timeout);//线程数量限制,IP地址,端口,超时时间
[*]
[*]}
[*]public static void addItemToList(String key,byte[] value)
[*]{
[*]Jedis jedis=null;
[*]try {
[*]jedis = pool.getResource();
[*]jedis.connect();
[*]jedis.select(DBIndex);
[*]jedis.lpush(key.getBytes(), value);
[*]
[*]} catch (Exception e) {
[*]e.printStackTrace();
[*]}
[*]finally{
[*]if(jedis!=null)
[*]pool.returnResource(jedis);
[*]}
[*]}
[*]@SuppressWarnings("finally")
[*]public static List<String> getItemFromList(String key)
[*]{
[*]Jedis jedis=null;
[*]//byte[] s=null;
[*]List<String> ss=null;
[*]try {
[*]jedis = pool.getResource();
[*]jedis.select(DBIndex);
[*]long len=jedis.llen(key);
[*]if(len==0) return null;
[*]ss = jedis.lrange(key, 0, (int)len);
[*]} catch (Exception e) {
[*]e.printStackTrace();
[*]
[*]}
[*]finally{
[*]if(jedis!=null)
[*]pool.returnResource(jedis);
[*]return ss;
[*]}
[*]
[*]}
[*]public static void addItem(String key,byte[] value)
[*]{
[*]Jedis jedis=null;
[*]try {
[*]jedis = pool.getResource();
[*]jedis.select(DBIndex);
[*]jedis.set(key.getBytes(), value);
[*]
[*]} catch (Exception e) {
[*]e.printStackTrace();
[*]}
[*]finally{
[*]if(jedis!=null)
[*]pool.returnResource(jedis);
[*]}
[*]
[*]}
[*]public static byte[] getItem(String key)
[*]{
[*]Jedis jedis=null;
[*]byte[] s=null;
[*]try {
[*]jedis = pool.getResource();
[*]jedis.select(DBIndex);
[*]s = jedis.get(key.getBytes());
[*]return s;
[*]} catch (Exception e) {
[*]e.printStackTrace();
[*]return s;
[*]}
[*]finally{
[*]if(jedis!=null)
[*]pool.returnResource(jedis);
[*]
[*]}
[*]
[*]
[*]}
[*]
[*]public static void delItem(String key)
[*]{
[*]Jedis jedis=null;
[*]try {
[*]jedis = pool.getResource();
[*]jedis.select(DBIndex);
[*]jedis.del(key.getBytes());
[*]
[*]} catch (Exception e) {
[*]e.printStackTrace();
[*]}
[*]finally{
[*]if(jedis!=null)
[*]pool.returnResource(jedis);
[*]}
[*]
[*]}
[*]public static long getIncrement(String key)
[*]{
[*]Jedis jedis=null;
[*]try {
[*]jedis = pool.getResource();
[*]jedis.select(DBIndex);
[*]return jedis.incr(key);
[*]
[*]} catch (Exception e) {
[*]e.printStackTrace();
[*]return 0L;
[*]}
[*]finally{
[*]if(jedis!=null)
[*]pool.returnResource(jedis);
[*]}
[*]
[*]}
[*]
[*]/**
[*]* 设置map 可以存储用户信息
[*]* @param key
[*]* @param map
[*]*/
[*]public static void setHashMap(String key,HashMap<String,String> map){
[*]Jedis jedis=null;
[*]try {
[*]jedis = pool.getResource();
[*]jedis.select(DBIndex);
[*]if(map!=null && !map.isEmpty()){
[*]for(Map.Entry<String, String> entry : map.entrySet()){
[*]jedis.hset(key, entry.getKey(), entry.getValue());
[*]}
[*]
[*]}
[*]} catch (Exception e) {
[*]e.printStackTrace();
[*]}finally{
[*]if(jedis!=null)
[*]pool.returnResource(jedis);
[*]}
[*]
[*]}
[*]
[*]public static Map<String,String> getHashMap(String key){
[*]Map<String,String> map = new HashMap<String,String>();
[*]Jedis jedis=null;
[*]try {
[*]jedis = pool.getResource();
[*]jedis.select(DBIndex);
[*]map = jedis.hgetAll(key);
[*]} catch (Exception e) {
[*]e.printStackTrace();
[*]}finally{
[*]if(jedis!=null)
[*]pool.returnResource(jedis);
[*]}
[*]return map;
[*]
[*]}
[*]
[*]/**
[*]* 添加set
[*]* @param key
[*]* @param set
[*]*/
[*]public static void addSet(String key,Set<String> set){
[*]Jedis jedis=null;
[*]try {
[*]jedis = pool.getResource();
[*]jedis.select(DBIndex);
[*]if(set!=null && !set.isEmpty()){
[*]for(String value : set){
[*]/*for ( Iterator<String> memberItr =
[*]jedis.smembers(str).iterator();//返回key对应set的所有元素,结果是无序的
[*]memberItr.hasNext();){
[*]final String member = memberItr.next();
[*]if (!jedis.sismember(str, member)){
[*]jedis.srem(str, member);
[*]}
[*]}*/
[*]jedis.sadd(key, value);
[*]}
[*]}
[*]} catch (Exception e) {
[*]e.printStackTrace();
[*]}finally{
[*]if(jedis!=null)
[*]pool.returnResource(jedis);
[*]}
[*]}
[*]
[*]public static Set<String> getSet(String key){
[*]Set<String> sets = new HashSet<String>();
[*]Jedis jedis=null;
[*]try {
[*]jedis = pool.getResource();
[*]jedis.select(DBIndex);
[*]sets = jedis.smembers(key);
[*]} catch (Exception e) {
[*]e.printStackTrace();
[*]}finally{
[*]if(jedis!=null)
[*]pool.returnResource(jedis);
[*]}
[*]
[*]return sets;
[*]}
[*]
[*]
[*]}
页: [1]
查看完整版本: redis的简单使用和介绍(转载)