Redis入门到精通只需要三篇博客
2019獨角獸企業重金招聘Python工程師標準>>>
(Redis介紹:略)
Redis-win-x64位程序的下載地址(缺分,分多的可以給我貢獻點):
http://download.csdn.net/download/qq_33601179/10165429
linux下的安裝百度一大堆,也不貼出來了,畢竟我沒用過,隨便貼一篇也不太好。
(1)win下安裝redis
非常簡單,只需要cmd命令行工具運行代碼,吧服務程序跑起來就OK,代碼為:(親測有效)
redis-server.exe redis.windows.conf
具體的安裝步驟,請看博客:
http://blog.csdn.net/jinwufeiyang/article/details/52156817
(2)簡單使用Redis
這個我也不廢話,直接看下面這個博客:(親測有效)
http://www.runoob.com/redis/redis-java.html
(3)性能優化
Redis連接池,Redis本身就自帶連接池。(親測有效)
推薦將所需要的配置參數寫到配置文件中,下面附上我封裝的一個Redis連接池的類,由三個部分組成:類BaseRedis(使用Redis連接池:JedisPool)、類PropertiesUtils(用于讀取配置文件)、redis.properties(參數配置文件)
import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig;/*** Created by Admin on 2017/12/19.*/ public class BaseRedis {private static JedisPool pool;//redis連接池/*** 從連接池中獲取對象** @return*/public static Jedis getJedis() {/*** 判定連接池是否已經初始化*/if (pool == null) {PropertiesUtils propertiesUtils=new PropertiesUtils("redis.properties");/*** 從配置文件中讀取配置參數*/Integer maxTotal=propertiesUtils.getValueForInt("maxTotal");if(maxTotal==null){maxTotal=100;}Integer maxWaitMillis=propertiesUtils.getValueForInt("maxWaitMillis");if(maxWaitMillis==null){maxWaitMillis=1000;}Integer maxIdle=propertiesUtils.getValueForInt("maxIdle");if(maxIdle==null){maxIdle=10;}Integer port=propertiesUtils.getValueForInt("port");if(port==null){port=6379;}String redisUrl=propertiesUtils.getValue("redisUrl");if(redisUrl==null){redisUrl="localhost";}// 建立連接池配置參數JedisPoolConfig config = new JedisPoolConfig();// 設置最大連接數config.setMaxTotal(maxTotal);// 設置最大阻塞時間,記住是毫秒數millisecondsconfig.setMaxWaitMillis(maxWaitMillis);// 設置空間連接config.setMaxIdle(maxIdle);// 創建連接池pool = new JedisPool(config, redisUrl, port);}return pool.getResource();}} import java.io.InputStream; import java.util.*;/*** 讀取配置文件中的屬性*/ public class PropertiesUtils {private String filePath;/*** 構造函數需要傳入待讀取的配置文件的名稱** @param propertiesFilePath*/public PropertiesUtils(String propertiesFilePath) {// ../../這個是根據當前類所在的目錄相對定位到配置文件的路徑,具體按需修改this.filePath = "../../../../" + propertiesFilePath;}/*** 讀取指定的配置參數** @param key* @return*/public String getValue(String key) {String str = "";try {Properties pro = new Properties();InputStream ins = this.getClass().getResourceAsStream(filePath);pro.load(ins);str = pro.getProperty(key);ins.close();} catch (Exception e) {e.printStackTrace();}return str;}/*** 讀取指定的配置參數** @param key* @return*/public Integer getValueForInt(String key) {String str = getValue(key);try {return Integer.parseInt(str);} catch (Exception e) {e.printStackTrace();}return null;}/*** 讀取所有的配置參數** @return*/public Map<String, String> getAllValue() {//讀取屬性文件a.propertiesMap<String, String> map = new HashMap<String, String>();try {Properties prop = new Properties();InputStream ins = this.getClass().getResourceAsStream(filePath);prop.load(ins); ///加載屬性列表Iterator<String> it = prop.stringPropertyNames().iterator();while (it.hasNext()) {String key = it.next();String value = prop.getProperty(key);map.put(key, value);}ins.close();} catch (Exception e) {e.printStackTrace();}return map;} } #redis配置文件 #URL redisUrl=localhost #端口號 port=6379 #最大連接數 maxTotal=100 #超時時間,單位毫秒 maxWaitMillis=1000 #最大xxx maxIdle=10?
轉載于:https://my.oschina.net/u/1462828/blog/1592455
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Redis入门到精通只需要三篇博客的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu14.04部署Ganglia
- 下一篇: 内存溢出之Tomcat内存配置