1. 程式人生 > >記錄一下:redis的安裝以及使用

記錄一下:redis的安裝以及使用

redis安裝:

[[email protected] setup]# yum -y install gcc gcc-c++                //安裝c編譯環境
[[email protected] setup]# wget http://download.redis.io/releases/redis-3.0.0.tar.gz
[[email protected] setup]# tar zxvf redis-3.0.0.tar.gz               //下載-解壓
[[email protected] setup]# cd redis-3.0.0
[[email protected]
redis-3.0.0]# make //編譯redis原始碼 [[email protected] redis-3.0.0]# make install PREFIX=/usr/local/redis //redis安裝目錄 redis的啟動: 前端啟動:在redis的安裝目錄下直接啟動redis-server [[email protected] bin]# ./redis-server 5475:C 18 Sep 03:40:29.101 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf 5475:M 18 Sep 03:40:29.104 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.0.0 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 //埠號 | `-._ `._ / _.-' | PID: 5475 //程式程序id `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io //官網地址 `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 5475:M 18 Sep 03:40:29.111 # Server started, Redis version 3.0.0 後臺啟動: 把redis-3.0.0/redis.conf複製到/usr/local/redis/bin目錄下 [
[email protected]
redis-3.0.0]# cp redis.conf /usr/local/redis/bin/ [[email protected] bin]# vim redis.conf //修改配置檔案 daemonize yes [[email protected] bin]# ./redis-server redis.conf //後臺啟動 [[email protected] bin]# ps aux | grep redis //檢視redis程序: root 4877 0.2 0.7 137444 7584 ? Ssl 02:53 0:07 ./redis-server *:6379 root 5465 0.0 0.0 103328 844 pts/1 S+ 03:39 0:00 grep redis

redis整合:

引入jar包:

       <jedis.version>2.7.2</jedis.version>

        <!-- Redis-jar -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

工具類:

package com.taotao.sso.jedis;
/**
 * jedis介面
 * @author Administrator
 *
 */
public interface JedisClient {

	String set(String key, String value);
	String get(String key);
	Boolean exists(String key);
	Long expire(String key, int seconds);
	Long ttl(String key);
	Long incr(String key);
	Long hset(String key, String field, String value);
	String hget(String key, String field);
	Long hdel(String key, String... field);
}


package com.taotao.sso.jedis;

import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
 * 單機版實現類
 * @author Administrator
 *
 */
public class JedisClientPool implements JedisClient {
	
	@Autowired
	private JedisPool jedisPool;

	@Override
	public String set(String key, String value) {
		Jedis jedis = jedisPool.getResource();
		String result = jedis.set(key, value);
		jedis.close();
		return result;
	}

	@Override
	public String get(String key) {
		Jedis jedis = jedisPool.getResource();
		String result = jedis.get(key);
		jedis.close();
		return result;
	}

	@Override
	public Boolean exists(String key) {
		Jedis jedis = jedisPool.getResource();
		Boolean result = jedis.exists(key);
		jedis.close();
		return result;
	}

	@Override
	public Long expire(String key, int seconds) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.expire(key, seconds);
		jedis.close();
		return result;
	}

	@Override
	public Long ttl(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.ttl(key);
		jedis.close();
		return result;
	}

	@Override
	public Long incr(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.incr(key);
		jedis.close();
		return result;
	}

	@Override
	public Long hset(String key, String field, String value) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.hset(key, field, value);
		jedis.close();
		return result;
	}

	@Override
	public String hget(String key, String field) {
		Jedis jedis = jedisPool.getResource();
		String result = jedis.hget(key, field);
		jedis.close();
		return result;
	}

	@Override
	public Long hdel(String key, String... field) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.hdel(key, field);
		jedis.close();
		return result;
	}

}


package com.taotao.sso.jedis;

import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.JedisCluster;
/**
 * 叢集版實現類
 * @author Administrator
 *
 */
public class JedisClientCluster implements JedisClient {
	
	@Autowired
	private JedisCluster jedisCluster;

	@Override
	public String set(String key, String value) {
		return jedisCluster.set(key, value);
	}

	@Override
	public String get(String key) {
		return jedisCluster.get(key);
	}

	@Override
	public Boolean exists(String key) {
		return jedisCluster.exists(key);
	}

	@Override
	public Long expire(String key, int seconds) {
		return jedisCluster.expire(key, seconds);
	}

	@Override
	public Long ttl(String key) {
		return jedisCluster.ttl(key);
	}

	@Override
	public Long incr(String key) {
		return jedisCluster.incr(key);
	}

	@Override
	public Long hset(String key, String field, String value) {
		return jedisCluster.hset(key, field, value);
	}

	@Override
	public String hget(String key, String field) {
		return jedisCluster.hget(key, field);
	}

	@Override
	public Long hdel(String key, String... field) {
		return jedisCluster.hdel(key, field);
	}

}

配置檔案:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	<context:annotation-config />
	<!-- <context:component-scan base-package=""/> -->
	<!-- 單機版 -->
	<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
		<constructor-arg name="host" value="192.168.52.139"></constructor-arg>
		<constructor-arg name="port" value="6379"></constructor-arg>
	</bean>
	<bean id="jedisClienPool" class="com.taotao.sso.jedis.JedisClientPool"></bean>
	<!-- redis叢集 -->
	<!-- <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"> <constructor-arg> 
		<set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" 
		value="192.168.25.153"/> <constructor-arg name="port" value="7001"/> </bean> 
		<bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" 
		value="192.168.25.153"/> <constructor-arg name="port" value="7002"/> </bean> 
		<bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" 
		value="192.168.25.153"/> <constructor-arg name="port" value="7003"/> </bean> 
		<bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" 
		value="192.168.25.153"/> <constructor-arg name="port" value="7004"/> </bean> 
		<bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" 
		value="192.168.25.153"/> <constructor-arg name="port" value="7005"/> </bean> 
		<bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" 
		value="192.168.25.153"/> <constructor-arg name="port" value="7006"/> </bean> 
		</set> </constructor-arg> </bean> <bean id="jedisClientCluster" class="JedisClientCluster"/> -->
</beans>

程式碼片段:

package com.taotao.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.IDUtils;
import com.taotao.common.utils.JsonUtils;
import com.taotao.jedis.JedisClient;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemExample;
import com.taotao.pojo.TbItemExample.Criteria;
import com.taotao.service.ItemService;


@Service
public class ItemServiceImpl implements ItemService {

	@Autowired
	private TbItemMapper itemMapper;
	@Autowired
	private TbItemDescMapper itemDescMapper;
	@Autowired
	private JmsTemplate jmsTemplate;
	@Autowired
	private JedisClient jedisClient;
	@Resource(name = "itemAddtopic")
	private Destination destination;

	@Value("${ITEM_INFO_PRE}")
	private String ITEM_INFO_PRE;
	@Value("${ITEM_INFO_EXPIRE}")
	private Integer ITEM_INFO_EXPIRE;

	/**
	 * 查詢商品詳細資訊
	 * @param itemId
	 * @return
	 */
	public TbItem getItemById(long itemId) {
		try {
			// 先查詢快取
			String json = jedisClient.get(ITEM_INFO_PRE + ":" + itemId + ":BASE");
			if (StringUtils.isNotBlank(json)) {
				// 把json轉換成java物件
				TbItem item = JsonUtils.jsonToPojo(json, TbItem.class);
				return item;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 根據商品id查詢商品資訊
		// TbItem item = itemMapper.selectByPrimaryKey(itemId);
		TbItemExample example = new TbItemExample();
		// 設定查詢條件
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(itemId);
		List<TbItem> list = itemMapper.selectByExample(example);
		if (list != null && list.size() > 0) {
			TbItem item = list.get(0);
			try {
				// 新增資料到快取
				jedisClient.set(ITEM_INFO_PRE + ":" + itemId + ":BASE", 
                JsonUtils.objectToJson(item));
				// 設定快取的有效期
				jedisClient.expire(ITEM_INFO_PRE + ":" + itemId + ":BASE", 
                ITEM_INFO_EXPIRE);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return item;
		}
		return null;
	}

	/**
	 * 查詢描述資訊
	 */
	public TbItemDesc getItemDescById(long itemId) {
		try {
			String json = jedisClient.get(ITEM_INFO_PRE + ":" + itemId + ":BASE");
			// 判斷是否命中
			if (StringUtils.isNotBlank(json)) {
				// 轉換為Java物件
				TbItemDesc itemDesc = JsonUtils.jsonToPojo(json, TbItemDesc.class);
				return itemDesc;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
		try {
			jedisClient.set(ITEM_INFO_PRE + ":" + itemId + ":BASE",         
            JsonUtils.objectToJson(itemDesc));
			// 設定過期時間
			jedisClient.expire(ITEM_INFO_PRE + ":" + itemId + ":BASE", ITEM_INFO_EXPIRE);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return itemDesc;
	}

	 
	 

}