赞
踩
- package com.cn.util
-
- import redis.clients.jedis.JedisPool
-
- object RedisUtils {
-
- private val host = "master"
- private val port = 6379
- //private val poolConfig = new GenericObjectPoolConfig()
- lazy val pool = new JedisPool(host, port)
-
- //关闭
- lazy val hooks = new Thread() {
- override def run(): Unit = {
- println("Execute hook thread: " + this)
- pool.destroy()
- }
- }
- }
- package com.cn.util
-
- import redis.clients.jedis.ScanParams
-
- import scala.util.control.Breaks.{break, breakable}
-
- object TestRedis {
-
- def main(args: Array[String]): Unit = {
- val jedis = RedisUtils.pool.getResource
- jedis.auth("123456")
- jedis.select(2)
- var scanRet = "0"
- val params = new ScanParams()
- params.`match`("*ll*")
- params.count(2)
- breakable {
- do {
- val ret = jedis.scan(scanRet, params)
- //返回下次遍历的游标
- scanRet = ret.getStringCursor
- val resultList = ret.getResult
- // val value = resultList.iterator()
- // while (value.hasNext){
- // println(value.next())
- // }
- resultList.toArray.foreach(x => println(x))
- } while (!scanRet.equals("0"))
- }
- jedis.close()
- }
- }
- hello
- ahellog
- hellog
- hellp
- package com.cn.until;
-
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
-
- /**
- *在高版本的jedis jar包,比如本版本2.9.0,JedisPoolConfig没有setMaxActive和setMaxWait属性了
- * 这是因为高版本中官方废弃了此方法,用以下两个属性替换。
- * maxActive ==> maxTotal
- * maxWait==> maxWaitMillis
- */
- public class RedisUtil {
- //创建连接池对象
- private static JedisPool pool=null;
- //设定连接超时时间
- private static int POOL_TIMEOUT = 10000;
- //创建连接池
- public static JedisPool getPool(String host,int port,String pwd){
- if(pool==null ){
- JedisPoolConfig config = new JedisPoolConfig();
- //设置jedis的最大实例数
- config.setMaxTotal(10);
- //设置jedis的最大空闲实例数
- config.setMaxIdle(5);
- // 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
- config.setMaxWaitMillis(1000 * 10);
- // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
- config.setTestOnBorrow(true);
-
- pool=new JedisPool(config,host,port,POOL_TIMEOUT,pwd);
- }
- return pool;
- }
-
- //关闭连接池
- public static void closePool(){
- if(pool!=null){
- pool.close();
- }
- }
-
- //归还jedis实例到连接池
- public static void returnJedis2Pool(Jedis jedis){
- if(jedis!=null){
- jedis.close();
- }
- }
- }
- package com.cn.service;
-
- import com.cn.until.RedisUtil;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.ScanParams;
- import redis.clients.jedis.ScanResult;
-
- import java.util.List;
-
- public class TestRedisScan {
- public static void main(String[] args) {
- String host="master";
- int port=6379;
- String pwd="123456";
- JedisPool pool = RedisUtil.getPool(host, port, pwd);
- Jedis jedis = pool.getResource();
- jedis.select(2);
-
- String scanRet="0";
- ScanParams scanParams = new ScanParams();
- //模糊查询
- scanParams.match("*ll*");
- //每次查询两条
- scanParams.count(2);
- do{
- ScanResult<String> ret = jedis.scan(scanRet, scanParams);
- //返回下次使用的游标
- scanRet = ret.getStringCursor();
- //返回结果集
- List<String> resultList = ret.getResult();
- for (String result:resultList) {
- System.out.println(result);
- }
- }while (!scanRet.equals("0"));
- //归还jedis实例
- RedisUtil.returnJedis2Pool(jedis);
- //关闭连接池
- RedisUtil.closePool();
- }
- }
- hello
- ahellog
- hellog
- hellp
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。