Java redis operations, javaredis

Source: Internet
Author: User

Java redis operations, javaredis
Java operations on redis simple Jedis instances

Package com. weixuan. utils; import redis. clients. jedis. jedis;/*** Create by fengtang * 2015/7/30 * JavaRedis */public final class JedisUtils {/*** Create a jedis link. ** @ return returns the currently created redis link object */public static Jedis getJedisConnection () {Jedis jedis = new Jedis ("localhost", 6379); return jedis ;}} // test package com. weixuan. test; import com. weixuan. utils. jedisPoolUtils; import com. weixuan. utils. jedisUtils; import org. junit. test; import redis. clients. jedis. jedis; import redis. clients. jedis. jedisPool;/*** Create by fengtang * 2015/7/30 * JavaRedis */public class JedisTest {@ Test public void testJedisUtils () {System. out. println ("testJedisUtils" + JedisUtils. getJedisConnection ());}}
Simple JedisPool Application

** Note: the Jedis version is 2.1, and the common pool version is 1.5.
In later versions, some parameters are set differently, such as setMaxActive **

Package com. weixuan. utils; import redis. clients. jedis. jedisPool; import redis. clients. jedis. jedisPoolConfig; import java. util. resourceBundle;/*** Create by fengtang * 2015/7/30 * JavaRedis */public class JedisPoolUtils {public static JedisPool pool; static {ResourceBundle bundle = ResourceBundle. getBundle ("jedis"); JedisPoolConfig config = new JedisPoolConfig (); if (bundle = null) {throw new Ille GalArgumentException ("[jedis. properties] is not found! ");} Config. setMaxActive (Integer. valueOf (bundle. getString ("redis. pool. maxActive "); config. setMaxIdle (Integer. valueOf (bundle. getString ("redis. pool. maxIdle "); config. setTestOnBorrow (Boolean. valueOf (bundle. getString ("redis. pool. testOnBorrow "); config. setTestOnReturn (Boolean. valueOf (bundle. getString ("redis. pool. testOnReturn "); pool = new JedisPool (config, bundle. getString ("redis. ip "), Integer. valueOf (bundle. getString ("redis. port ");} public static JedisPool getPool () {return pool ;}// test package com. weixuan. test; import com. weixuan. utils. jedisPoolUtils; import com. weixuan. utils. jedisUtils; import org. junit. test; import redis. clients. jedis. jedis; import redis. clients. jedis. jedisPool;/*** Create by fengtang * 2015/7/30 * JavaRedis */public class JedisTest {@ Test public void testJedisPoolUtils () throws Exception {JedisPool = JedisPoolUtils. getPool (); System. out. println ("testJedisPoolUtils --- pool ------------" + pool); Jedis jedis = pool. getResource (); System. out. println ("testJedisPoolUtils --- jedis ------------" + jedis );}}

Test in a later version

* Jedis version 2.7 common pool version 2.4 *

Set config. setMaxActive (Integer. valueOf (bundle. getString ("redis. pool. maxActive "); change to config. setMaxTotal (Integer. valueOf (bundle. getString ("redis. pool. maxTotal "); the configuration file also needs to be modified.

Easy to use

    package com.weixuan.redis;    import com.weixuan.utils.JedisPoolUtils;    import redis.clients.jedis.Jedis;    /**     * Create by fengtang     * 2015/7/30     * JavaRedis3     */    public class Main {    private Jedis jedis = JedisPoolUtils.getPool().getResource();    public void testInsert() {        jedis.set("testKey1", "testValue1");        jedis.set("testKey2", "testValue2");        jedis.set("testKey3", "testValue3");        jedis.set("testKey4", "testValue4");        jedis.set("testKey5", "testValue5");        jedis.set("testKey6", "testValue6");        jedis.set("testKey7", "testValue7");        jedis.set("testKey8", "testValue8");        jedis.set("testKey9", "testValue9");        jedis.set("testKey0", "testValue0");    }    public String testGet(String keyName) {        return jedis.get(keyName);    }    public static void main(String[] args) {        //new Main().testInsert();        System.out.println(new Main().testGet("testKey0"));    }}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.