`

redis demo

 
阅读更多

 

下载redis windows版:

redis-2.4.5-win32-win64.zip

开启 redis-server 服务

 

1.pom.xml 

<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
		</dependency>

		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>

		<!--Redis -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.0.0</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
	</dependencies>

 

2.JedisPool

/**
 * jedis池使用
 *
 */
public class MyJedisPool {
	// jedis池
	private static JedisPool pool;
	// jedis实例
	private static Jedis jedis;

	// 静态代码初始化池配置
	static {
		// 加载redis配置文件
		ResourceBundle bundle = ResourceBundle.getBundle("redis");
		if (bundle == null) {
			throw new IllegalArgumentException("[redis.properties] is not found!");
		}
		// 创建jedis池配置实例
		JedisPoolConfig config = new JedisPoolConfig();
		// 设置池配置项值
		config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive")));
		config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
		config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));
		config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));
		config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));
		// 根据配置实例化jedis池
		pool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")));
		// 从jedis池中获取一个jedis实例
		jedis = pool.getResource();
	}

	/**
	 * 测试jedis池方法
	 */
	@Test
	public void test1() {

		// 获取jedis实例后可以对redis服务进行一系列的操作
		jedis.set("name", "xmong");
		System.out.println(jedis.get("name"));
		jedis.del("name");
		System.out.println(jedis.exists("name"));

		// 释放对象池,即获取jedis实例使用后要将对象还回去
		pool.returnResource(jedis);
	}

	/**
	 * Redis存储初级的字符串 CRUD
	 */
	@Test
	public void testBasicString() {
		// -----添加数据----------
		jedis.set("name", "minxr");// 向key-->name中放入了value-->minxr
		System.out.println(jedis.get("name"));// 执行结果:minxr

		// -----修改数据-----------
		// 1、在原来基础上修改
		jedis.append("name", "jarorwar"); // 很直观,类似map 将jarorwar append到已经有的value之后
		System.out.println(jedis.get("name"));// 执行结果:minxrjarorwar

		// 2、直接覆盖原来的数据
		jedis.set("name", "闵晓荣");
		System.out.println(jedis.get("name"));// 执行结果:闵晓荣

		// 删除key对应的记录
		jedis.del("name");
		System.out.println(jedis.get("name"));// 执行结果:null

		/**
		 * mset相当于 jedis.set("name","minxr"); jedis.set("jarorwar","闵晓荣");
		 */
		jedis.mset("name", "minxr", "jarorwar", "闵晓荣");
		System.out.println(jedis.mget("name", "jarorwar"));

	}

	/**
	 * jedis操作Map
	 */
	@Test
	public void testMap() {
		Map<String, String> user = new HashMap<String, String>();
		user.put("name", "minxr");
		user.put("pwd", "password");
		jedis.hmset("user", user);
		// 取出user中的name,执行结果:[minxr]-->注意结果是一个泛型的List
		// 第一个参数是存入redis中map对象的key,后面跟的是放入map中的对象的key,后面的key可以跟多个,是可变参数
		List<String> rsmap = jedis.hmget("user", "name");
		System.out.println(rsmap);

		// 删除map中的某个键值
		// jedis.hdel("user","pwd");
		System.out.println(jedis.hmget("user", "pwd")); // 因为删除了,所以返回的是null
		System.out.println(jedis.hlen("user")); // 返回key为user的键中存放的值的个数1
		System.out.println(jedis.exists("user"));// 是否存在key为user的记录 返回true
		System.out.println(jedis.hkeys("user"));// 返回map对象中的所有key [pwd, name]
		System.out.println(jedis.hvals("user"));// 返回map对象中的所有value [minxr, password]

		Iterator<String> iter = jedis.hkeys("user").iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			System.out.println(key + ":" + jedis.hmget("user", key));
		}

	}

	/**
	 * jedis操作List
	 */
	@Test
	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));
	}

	/**
	 * jedis操作Set
	 */
	@Test
	public void testSet() {
		// 添加
		jedis.sadd("sname", "minxr");
		jedis.sadd("sname", "jarorwar");
		jedis.sadd("sname", "闵晓荣");
		jedis.sadd("sname", "noname");
		// 移除noname
		jedis.srem("sname", "noname");
		System.out.println(jedis.smembers("sname"));// 获取所有加入的value
		System.out.println(jedis.sismember("sname", "minxr"));// 判断 minxr 是否是sname集合的元素
		System.out.println(jedis.srandmember("sname"));
		System.out.println(jedis.scard("sname"));// 返回集合的元素个数
	}

	@Test
	public void test() throws InterruptedException {
		// keys中传入的可以用通配符
		System.out.println(jedis.keys("*")); // 返回当前库中所有的key [sose, sanme, name, jarorwar, foo, sname, java framework,
												// user, braand]
		System.out.println(jedis.keys("*name"));// 返回的sname [sname, name]
		System.out.println(jedis.del("sanmdde"));// 删除key为sanmdde的对象 删除成功返回1 删除失败(或者不存在)返回 0
		System.out.println(jedis.ttl("sname"));// 返回给定key的有效时间,如果是-1则表示永远有效
		jedis.setex("timekey", 10, "min");// 通过此方法,可以指定key的存活(有效时间) 时间为秒
		Thread.sleep(5000);// 睡眠5秒后,剩余时间将为<=5
		System.out.println(jedis.ttl("timekey")); // 输出结果为5
		jedis.setex("timekey", 1, "min"); // 设为1后,下面再看剩余时间就是1了
		System.out.println(jedis.ttl("timekey")); // 输出结果为1
		System.out.println(jedis.exists("key"));// 检查key是否存在
		System.out.println(jedis.rename("timekey", "time"));
		System.out.println(jedis.get("timekey"));// 因为移除,返回为null
		System.out.println(jedis.get("time")); // 因为将timekey 重命名为time 所以可以取得值 min

		// jedis 排序
		// 注意,此处的rpush和lpush是List的操作。是一个双向链表(但从表现来看的)
		jedis.del("a");// 先清除数据,再加入数据进行测试
		jedis.rpush("a", "1");
		jedis.lpush("a", "6");
		jedis.lpush("a", "3");
		jedis.lpush("a", "9");
		System.out.println(jedis.lrange("a", 0, -1));// [9, 3, 6, 1]
		System.out.println(jedis.sort("a")); // [1, 3, 6, 9] //输入排序后结果
		System.out.println(jedis.lrange("a", 0, -1));
	}
}

 

3. redis.properties

#最大分配的对象数
redis.pool.maxActive=1024
#最大能够保错误的Unicode字符串!
redis.pool.maxIdle=200
#当池内没有返回对象时,最大等待时间
redis.pool.maxWait=1000
#当调错误的Unicode字符串!
redis.pool.testOnBorrow=true
#当调错误的Unicode字符串!
redis.pool.testOnReturn=true
#IP
redis.ip=127.0.0.1
#Port
redis.port=6379

 

 另外,补充 redis 的工具类

 JedisUtil

import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public final class JedisUtil {
	private static final Logger LOGGER = Logger.getLogger(JedisUtil.class);
	private static int DEFAULT_DB_INDEX = 0;

	private static JedisPool jedisPool = null;

	private JedisUtil() {
		// private constructor
	}

	private static void initialPool() {
		try {
			ResourceBundle bundle = ResourceBundle.getBundle("redis");
			if (bundle == null) {
				throw new IllegalArgumentException("[redis.properties] is not found!");
			}
			// 创建jedis池配置实例
			JedisPoolConfig config = new JedisPoolConfig();
			// 设置池配置项值
			String address = bundle.getString("redis.ip");
			int port = Integer.valueOf(bundle.getString("redis.port"));
			LOGGER.info("Redis server info: " + address + ":" + port);

			String strDbIndex = bundle.getString("redis.db_index");
			if (strDbIndex != null) {
				DEFAULT_DB_INDEX = Integer.valueOf(strDbIndex);
			}

			String strMaxActive = bundle.getString("redis.pool.maxActive");
			if (strMaxActive != null) {
				config.setMaxActive(Integer.valueOf(strMaxActive));
			}

			String strMaxIdle = bundle.getString("redis.pool.maxIdle");
			if (strMaxIdle != null) {
				config.setMaxIdle(Integer.valueOf(strMaxIdle));
			}

			String strMaxWait = bundle.getString("redis.pool.maxWait");
			if (strMaxWait != null) {
				config.setMaxWait(Long.valueOf(strMaxWait));
			}

			String strTestOnBorrow = bundle.getString("redis.pool.testOnBorrow");
			if (strTestOnBorrow != null) {
				config.setTestOnBorrow(Boolean.valueOf(strTestOnBorrow));
			}

			String strTestOnReturn = bundle.getString("redis.pool.testOnReturn");
			if (strTestOnReturn != null) {
				config.setTestOnReturn(Boolean.valueOf(strTestOnReturn));
			}

			String strTimeout = bundle.getString("redis.pool.timeout");
			int timeout = 2000;// 默认2000
			if (strTimeout != null) {
				timeout = Integer.valueOf(strTimeout);
			}
			// 根据配置实例化jedis池
			jedisPool = new JedisPool(config, address, port, timeout);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}

	}

	public synchronized static Jedis getJedisInstance() {
		if (jedisPool == null) {
			initialPool();
		}
		try {
			if (jedisPool != null) {
				Jedis resource = jedisPool.getResource();
				resource.select(DEFAULT_DB_INDEX);
				return resource;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public synchronized static Jedis getJedisInstance(final int dbIndex) {
		if (jedisPool == null) {
			initialPool();
		}
		try {
			if (jedisPool != null) {
				Jedis resource = jedisPool.getResource();
				resource.select(dbIndex);
				return resource;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void returnResource(final Jedis jedis) {
		if (jedis != null) {
			jedisPool.returnResource(jedis);
		}
	}
}

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics