标签:
前言
本文主要包含以下三个方面的内容:Redis安装及配置、Redis基本操作、小结
一、Redis安装及配置
(1)环境
OS : CentOS 6.5 官网下载对应版本: http://www.centoscn.com/CentosSoft/iso/
Redis: Redis 3.0.3 官网下载对应版本: http://redis.io/download
(2)安装步骤
安装CentOS 6.5 的过程省略。默认为已经有CentOS 6.5 的操作环境,安装Redis 的步骤如下
1> 下载Redis 稳定版本
wget http://download.redis.io/redis-stable.tar.gz
2> 解压
tar xvzf redis-stable.tar.gz
3> 进入redis-stable 文件夹
cd redis-stable
4> 开始编译redis
make
5> 进入到src文件夹
cd src
6> 安装redis
make install
7>测试redis
make test
8>运行时如果出错,大概是tcl版本太低的原因的话请按照下面的步骤运行,然后再运行 make test 命令。最终会显示运行成功!
wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz sudo tar xzvf tcl8.6.1-src.tar.gz -C /usr/local/ cd /usr/local/tcl8.6.1/unix/ sudo ./configure sudo make sudo make install
9>安装完毕之后,运行如下命令,就可以启动Redis了
redis-server
10> redis启动之后,运行客户端命令就可以连接到redis上
redis-cli
11>连接到redis之后可以运行如下的命令对数据进行增删改查
add key test /* add data */ get key /* get data */ del key /* del data */
(3)配置说明
在文件夹redis-stable中找到 redis.conf文件,就可以对redis进行相关配置了,可以根据自己的需要进行相关的配置。
比如需要添加密码,则在redis.conf文件中 有“#requirepass ”去掉“#” ,然后再关键字requirepass 后添加密码。
二、Redis对数据的操作
对redis的操作可以通过不同的方式进行,比如直接通过命令行的方式对数据进行操作、通过不同语言作为客户端对其实现操作数据。本文以Java为例来实现对redis数据的增删改查。使用了第三方库
package com.cx.redis;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* @description :
*
* @author : WuBo
*
* @date : 2015年8月4日
*
*/
public class RedisUtil {
private static JedisPool pool = null;
/**
* @desc 获取连接池
* @return
*/
public static JedisPool getPool() {
if (pool == null) {
JedisPoolConfig config = new JedisPoolConfig();
//config.setMaxIdle(5);
config.setMaxTotal(1000);//最大连接数
//config.setMaxWaitMillis(1000 * 100);
config.setTestOnBorrow(true); //测试连接是否可用
pool = new JedisPool(config, "192.168.1.100", 6379,1000 * 100,"xxxx123");
}
return pool;
}
/**
* @desc 释放连接
* @return
*/
public static void returnResource(JedisPool pool, Jedis redis) {
if (redis != null) {
pool.returnResourceObject(redis);
}
}
/**
* @desc 获取值
* @return
*/
public static String get(String key){
String value = null;
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.get(key);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
} finally {
//返还到连接池
returnResource(pool, jedis);
}
return value;
}
/**
* @desc 随机获取制定set中的元素
* @param key
* @return
*/
public static String getRandEleOfSet(String key){
String value = null;
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.srandmember(key);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
} finally {
//返还到连接池
returnResource(pool, jedis);
}
return value;
}
/**
* @desc 随机获取制定set中的所有元素
* @param key
* @return
*/
public static Set<String> getAllEleOfSet(String key){
Set<String> value = null;
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.smembers(key);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
} finally {
//返还到连接池
returnResource(pool, jedis);
}
return value;
}
//清空缓存数据
public static void clearCache(String key){
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.del(key);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* @desc: 添加一个SET集合
*/
public static void addElementToSet(String key,String... element){
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.sadd(key, element);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* @desc 获取redis连接
* @return
*/
public static Jedis getJedis(){
Jedis jedis = null;
try{
jedis = getPool().getResource();
}catch(Exception e){
pool.returnBrokenResource(jedis);
e.printStackTrace();
}
return jedis;
}
}
三、小结
本文对Redis的安装、配置、以及相关运用做了一个简单总结作为自己的一个记录吧。
标签:
原文地址:http://www.cnblogs.com/macsnow/p/4772050.html