How Redis is used in Java

Source: Internet
Author: User

1. First download the jar package and put it in your project

2. Practice

Package com.jianyuan.redisTest;

Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Set;

Import Redis.clients.jedis.Jedis;

public class Redistest {

public static void Main (string[] args) {
Connect to a local Redis service
Jedis Jedis = new Jedis ("127.0.0.1", 6379);
Authority authentication
Jedis.auth ("wenhongyu66");
Jedis.select (0);
SYSTEM.OUT.PRINTLN ("Connection succeeded");
See if the service is running
System.out.println (Jedis.ping ());

Setting up Redis string data
Jedis.set ("Runoobkey", "www.runoob.com");
Get the stored data and output
System.out.println ("redis-stored string:" + jedis.get ("Runoobkey"));

Storing data in a list
Jedis.lpush ("Site-list", "Runoob");
Jedis.lpush ("Site-list", "Google");
Jedis.lpush ("Site-list", "Taobao");
System.out.println (Jedis.llen ("site-list"));
Get the stored data and output
list<string> list = Jedis.lrange ("Site-list", 0, Jedis.llen ("site-list"));
for (int i=0; i<list.size (); i++) {
SYSTEM.OUT.PRINTLN ("list item is:" +list.get (i));
}

Get the data and output
set<string> keys = Jedis.keys ("*");
Iterator<string> It=keys.iterator ();
while (It.hasnext ()) {
String key = It.next ();
SYSTEM.OUT.PRINTLN (key);
}

}

}

Summary: You can encapsulate some tool classes for ease of use, including connection pool configuration, Jedis parameter configuration and so on.

private static JedisPool jedisPool = null ; private static Jedis jedis; static {    jedis = getJedisPool().getResource(); } /**   * 构建redis连接池   */ public static JedisPool getJedisPool() {    if (jedisPool == null ) {      JedisPoolConfig config = new JedisPoolConfig();      config.setMaxTotal( 1024 ); // 可用连接实例的最大数目,如果赋值为-1,表示不限制.      config.setMaxIdle( 5 ); // 控制一个Pool最多有多少个状态为idle(空闲的)jedis实例,默认值也是8      config.setMaxWaitMillis( 1000 * 100 ); // 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时/如果超过等待时间,则直接抛出异常      config.setTestOnBorrow( true ); // 在borrow一个jedis实例时,是否提前进行validate操作,如果为true,则得到的jedis实例均是可用的      jedisPool = new JedisPool(config, "127.0.0.1" , 6379 );    }    return jedisPool; } /**   * 释放jedis资源   */ public static void returnResource(Jedis jedis) {    if (jedis != null ) {      jedis.close();    } } public static String get(String key) {    String value = null ;    Jedis jedis = null ;    try {      JedisPool pool = getJedisPool();      jedis = pool.getResource();      value = jedis.get(key);    }    catch (Exception e) {      returnResource(jedis);      e.printStackTrace();    }    finally {      returnResource(jedis);    }    return value; }The Redistemplate encapsulates the Jedis from the Jedispool and returns to the pool public class RedisTemplate {    private JedisPool jedisPool;    public RedisTemplate(JedisPool jedisPool) {      this .jedisPool = jedisPool;    }    public <T> T execute(RedisCallback<T> callback) {      Jedis jedis = jedisPool.getResource();      try {        return callback.handle(jedis);      }      catch (Exception e) {        // throw your exception        throw e;      }      finally {        returnResource(jedis);      }    }    private void returnResource(Jedis jedis) {      if (jedis != null ) {        jedis.close();      }    } } public interface RedisCallback<T> {    public T handle(Jedis jedis); }

How Redis is used in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.