java中使用redis

來源:互聯網
上載者:User

標籤:redis伺服器   res   ace   預設   對象   var   val   api   span   

java中使用redis:

1:簡述jedis與redis的關係:

      jedis簡述:

即:jedis是串連redis的橋樑-----用戶端

     jedis 的命名:java   redis ---->jedis

 

二、下面我們我們看一看jedis的具體使用:

需要的jar包:

 

 

2-1:單一實例串連:

1 public class RedisDemo {2     public static void main(String[] args) {3         Jedis jedis = new Jedis("39.106.19.224", 6379);
jedis.set("hello","javaRedis");4 String hello = jedis.get("hello");5 System.out.println(hello);
jedis.close();6 }7 8 }

 

 2-2:串連池串連:

常用的ApI:

 1 public void jedisPool() { 2         //建立串連池設定物件: 3         JedisPoolConfig jpc = new JedisPoolConfig(); 4         //設定最大閑置個數 5         jpc.setMaxIdle(30); 6         //設定最小閑置個數: 7         jpc.setMinIdle(10); 8         //設定最大的串連數 9         jpc.setMaxTotal(50);10         //建立串連池對象  host:串連redis主機IP ;port:redis的預設連接埠11         JedisPool jedisPool = new JedisPool(jpc, "127.0.0.1", 6379);12         //擷取串連資源13         Jedis resource = jedisPool.getResource();14         //放值:15         resource.set("key", "你好redis");16         //取值:17         resource.get("key");18     }

 

 

 

 

java中使用redis的工具類:

JedisPoolUtils  :

 1 import redis.clients.jedis.Jedis; 2 import redis.clients.jedis.JedisPool; 3 import redis.clients.jedis.JedisPoolConfig; 4  5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.util.Properties; 8  9 public class JedisPoolUtils {10     private static JedisPool pool = null;11 12     static {13         //載入設定檔14         InputStream in = JedisPoolUtils.class.getClassLoader().getResourceAsStream("redis.properties");15         Properties pro = new Properties();16         try {17             pro.load(in);18         } catch (IOException e) {19             e.printStackTrace();20         }21         //獲得池子物件22         JedisPoolConfig poolConfig = new JedisPoolConfig();23         poolConfig.setMaxIdle(Integer.parseInt(pro.get("redis.maxIdle").toString()));//最大閑置個數24         poolConfig.setMinIdle(Integer.parseInt(pro.get("redis.minIdle").toString()));//最小閑置個數25         poolConfig.setMaxTotal(Integer.parseInt(pro.get("redis.maxTotal").toString()));//最大串連數26         pool = new JedisPool(poolConfig, pro.getProperty("redis.url"), Integer.parseInt(pro.get("redis.port").toString()));27     }28 29     //獲得jedis資源的方法30     public static Jedis getJedis() {31         return pool.getResource();32     }33 34     //存值:35     public static void setPool(String key,String value){36         getJedis().set(key,value);37     }38     //取值:39     public static void getPool(String key){40         getJedis().get(key);41     }42     public static void main(String[] args) {43         setPool("key","value");44         getPool("key");45     }46 }

 

redis.properties  :

 1 #最大閑置數 2 redis.maxIdle=30 3 #最小閑置數 4 redis.minIdle=10 5 #最大串連數 6 redis.maxTotal=100 7 #串連安裝redis伺服器的IP地址 8 redis.url=192.168.186.131 9 #redis的預設連接埠10 redis.port=6379

 

java中使用redis

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.