redis在linux中安装,java端代码jedis和jedisPool

来源:互联网 发布:seo推广 编辑:程序博客网 时间:2024/06/11 20:57
redis安装 linux

        redis-2.6.16.tar.gz
        
        确保linux中已安装好gcc,否则 yum gcc
        
        解压  安装
        
        make
        //指定安装目录,直接安装
        make  PREFIX=/usr/local/redis install
        注意如果出错::
            1:可能是版本问题
            2:使用
                     make CFLAGS="-march=i686" PREFIX=/usr/local/redis install
                来安装;
        
        安装之后;将redis.conf 复制到/usr/local/redis下
        启动服务器:./bin/redis-server
        正常启动:则使用./bin/redis-server  redis.conf 重启让服务后台运行
        启动客户端:./bin/redis-cli
        测试服务器与客户端的联通:ping 返回 pong
        测试成功:
        
        java接口
        单机版:
        适合访问量小要求不高的缓存;
        直接调用Jedis不能满足客户端的需求
        需要使用连接池JedisPool
        从连接池JedisPool中获取jedis
        JedisPool.java
        
        import redis.clients.jedis.Jedis;
        import redis.clients.jedis.JedisPool;
        import redis.clients.jedis.JedisPoolConfig;

        public class JedisPoolUtils {

            private static JedisPool pool;

            /**
             * 建立连接池 真实环境,一般把配置参数缺抽取出来。
             *
             */
            private static void createJedisPool() {

                // 建立连接池配置参数
                JedisPoolConfig config = new JedisPoolConfig();

                // 设置最大连接数
                config.setMaxActive(100);

                // 设置最大阻塞时间,记住是毫秒数milliseconds
                config.setMaxWait(1000);

                // 设置空间连接
                config.setMaxIdle(10);

                // 创建连接池
                pool = new JedisPool(config, "127.0.0.1", 6379);

            }

            /**
             * 在多线程环境同步初始化
             */
            private static synchronized void poolInit() {
                if (pool == null)
                    createJedisPool();
            }

            /**
             * 获取一个jedis 对象
             *
             * @return
             */
            public static Jedis getJedis() {

                if (pool == null)
                    poolInit();
                return pool.getResource();
            }

            /**
             * 归还一个连接
             *
             * @param jedis
             */
            public static void returnRes(Jedis jedis) {
                pool.returnResource(jedis);
            }

    在使用redis存储对象时应注意:
    redis存储的形式是:key--value
    如果想存入对象则需要序列化,
        即:将对像序列化后才能存进redis
        但是,取值的时候又要反序列化才能正常显示
    所以redis存储对象时需要用到序列化和反序列化
    SerializeUtil.java

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;

    public class SerializeUtil {

        public static byte[] serialize(Object object) {

        ObjectOutputStream oos = null;

        ByteArrayOutputStream baos = null;

        try {

        //序列化

        baos = new ByteArrayOutputStream();

        oos = new ObjectOutputStream(baos);

        oos.writeObject(object);

        byte[] bytes = baos.toByteArray();

        return bytes;

        } catch (Exception e) {

        

        }

        return null;

        }

        

        public static Object unserialize(byte[] bytes) {

        ByteArrayInputStream bais = null;

        try {

        //反序列化

        bais = new ByteArrayInputStream(bytes);

        ObjectInputStream ois = new ObjectInputStream(bais);

        return ois.readObject();

        } catch (Exception e) {

        

        }

        return null;

        }

        目前已经可以正常存取对象了;
        但是连接池中的一些常量需要手动添加,修改频繁呢??
        所以需要将redis.properties中的数据读取出来所以又用到:
        ReadPropertise.java
        
        import java.io.FileNotFoundException;



public class ReadProperties {
        final static Logger logger = LoggerFactory.getLogger(MongoConfig.class);
        static final String DEFAULT_CONFIGURATION_FILE = "redis.properties";
        private static JedisPool pool;
        static Properties props = new Properties();
        private static String redisHost;
        private static int redisPort;
        private static String redisPass;
        private static int redisMaxIdle;
        private static int redisMinIdle;
        private static int redisMaxActive;
        private static int redisMaxWait;
        private static boolean testOnBorrow;
        
        static {
            init();
        }
        /**
        * 初始化配置文件
        */
        public static void init() {
            logger.info("Reading configuration from default configuration file [{}]",
                    DEFAULT_CONFIGURATION_FILE);
            InputStream is = null;
            try {
                is = new AppConfig().getClass().getResourceAsStream("/resource/redis.properties");
                props.load(is);
            } catch (FileNotFoundException ex) {
                logger.error("Could not read configuration file [{}]",
                        DEFAULT_CONFIGURATION_FILE);
                logger.error("Ignoring configuration file [{}]",
                        DEFAULT_CONFIGURATION_FILE);
            } catch (IOException ex) {
                logger.error("Could not read configuration file [{}]",
                        DEFAULT_CONFIGURATION_FILE);
                logger.error("Ignoring configuration file [{}]",
                        DEFAULT_CONFIGURATION_FILE);
            } catch (Exception ex) {
                logger.error("Could not read configuration file [{}]",
                        DEFAULT_CONFIGURATION_FILE);
                logger.error("Ignoring configuration file [{}]",
                        DEFAULT_CONFIGURATION_FILE);
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (Exception ignore) {
                    }
                }
            }
            doConfigure(props);
        }
        
        /**
         * 读取配置文件
         * @param props
         */
        public static void doConfigure(Properties props) {
            redisHost= props.getProperty("redis.host");
            redisPort = Integer.parseInt(props.getProperty("redis.port"));
            redisPass= props.getProperty("redis.pass");
            redisMaxIdle = Integer.parseInt(props.getProperty("redis.maxIdle"));
            redisMinIdle = Integer.parseInt(props.getProperty("redis.minIdle"));
            redisMaxActive = Integer.parseInt(props.getProperty("redis.maxActive"));
            redisMaxWait = Integer.parseInt(props.getProperty("redis.maxWait"));
            testOnBorrow = props.getProperty("redis.testOnBorrow").equals("true")?true:false;
        }
        
}

}

    读取到所需数据后将数据赋给连接池jedisPool中config设置的值;
    ok那么就可以实现简单的redis读写了

    
    
    
    
    
        
        
   
0 0
原创粉丝点击