1 首先在srpingMVP项目中配置好spring_xml文件
<!-- 配置文件信息加载 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/properties/redis.properties</value>
</list>
</property>
</bean>
<!-- redis连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxActive" value="${mouap.redis.pool.maxActive}" />
<property name="maxIdle" value="${mouap.redis.pool.maxIdle}" />
<property name="maxWait" value="${mouap.redis.pool.maxWait}" />
<property name="testOnBorrow" value="${mouap.redis.pool.testOnBorrow}" />
</bean>
<!-- redis连接工厂 -->
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${mouap.redis.ip}" />
<property name="port" value="${mouap.redis.port}" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer">
</bean>
<!-- redis 模板 -->
<bean id="redisTemplate" name="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory" p:keySerializer-ref="stringRedisSerializer"
p:hashKeySerializer-ref="stringRedisSerializer" />
2 然后建立redis.properties文件
mouap.redis.maxActive=200
mouap.redis.pool.maxIdle=50
mouap.redis.pool.maxWait=3000
mouap.redis.pool.testOnBorrow=true
#这里的IP为虚拟机的IP
mouap.redis.ip=192.168.0.47
#Port 这里为虚拟机redis的端口
mouap.redis.port=6379
3 在项目中建立redis帮助类
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
import java.util.Set;
//抽象类
public interface CacheService {
/**
* 通过key删除
*
* @param key
*/
public abstract long del(String... keys);
/**
* 添加key value 并且设置存活时间(byte)
*
* @param key
* @param value
* @param liveTime
*/
public abstract void set(byte[] key, byte[] value, long liveTime);
/**
* 添加key value 并且设置存活时间
*
* @param key
* @param value
* @param liveTime
* 单位秒
*/
public abstract void set(String key, String value, long liveTime);
/**
* 添加key value
*
* @param key
* @param value
*/
public abstract void set(String key, String value);
/**
* 添加key value (字节)(序列化)
*
* @param key
* @param value
*/
public abstract void set(byte[] key, byte[] value);
/**
* 获取redis value (String)
*
* @param key
* @return
*/
public abstract String get(String key);
/**
* 通过正则匹配keys
*
* @param pattern
* @return
* @return
*/
public abstract Set<?> Setkeys(String pattern);
/**
* 检查key是否已经存在
*
* @param key
* @return
*/
public abstract boolean exists(String key);
/**
* 删除当前操作的数据库中的key
*
* @return
*/
public abstract String flushDB();
/**
* 查看redis里有多少数据
*/
public abstract long dbSize();
/**
* 检查是否连接成功
*
* @return
*/
public abstract String ping();
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
import java.io.UnsupportedEncodingException;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
/**
* 缓存业务处理类
*
* @author devintam
*
* @date 2015-07-02
*
*/
@Service
public class CacheServiceImpl implements CacheService {
public Log logger = LogFactory.getLog(this.getClass());
@Autowired
private RedisTemplate<String, Object> redisTemplate;
private static String redisCode = "utf-8";
/**
* 删除缓存
*
* @param keys
* 单个/keys数组
* @return long
*/
public long del(final String... keys) {
return (Long) redisTemplate.execute(new RedisCallback<Object>() {
public Long doInRedis(RedisConnection connection)
throws DataAccessException {
long result = 0;
for (int i = 0; i < keys.length; i++) {
result = connection.del(keys[i].getBytes());
}
return result;
}
});
}
/**
* 设置缓存-byte
*
* @param key
* 缓存key值
* @param value
* 缓存value值
* @param liveTime
* 存活时长-单位秒
*/
public void set(final byte[] key, final byte[] value, final long liveTime) {
redisTemplate.execute(new RedisCallback<Object>() {
public Long doInRedis(RedisConnection connection)
throws DataAccessException {
connection.setNX(key, value);
if (liveTime > 0) {
connection.expire(key, liveTime);
}
return 1L;
}
});
}
/**
* 设置缓存-String
*
* @param key
* 缓存key值
* @param value
* 缓存value值
* @param liveTime
* 存活时长-单位秒
*/
public void set(String key, String value, long liveTime) {
this.set(key.getBytes(), value.getBytes(), liveTime);
}
/**
* 设置缓存-String
*
* @param key
* 缓存key值
* @param value
* 缓存value值
*/
public void set(String key, String value) {
this.set(key, value, 0L);
}
/**
* 设置缓存-byte
*
* @param key
* 缓存key值
* @param value
* 缓存value值
*/
public void set(byte[] key, byte[] value) {
this.set(key, value, 0L);
}
/**
* 获取缓存
*
* @param key
* 缓存key值
* @return String
*/
public String get(final String key) {
return (String) redisTemplate.execute(new RedisCallback<Object>() {
public String doInRedis(RedisConnection connection)
throws DataAccessException {
try {
return new String(connection.get(key.getBytes()), redisCode);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
});
}
/**
* 是否存在cache
*
* @param key
* 缓存key值
* @return boolean
*/
public boolean exists(final String key) {
return (Boolean) redisTemplate.execute(new RedisCallback<Object>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
return connection.exists(key.getBytes());
}
});
}
/**
* 删除当前操作的数据库中的key
*
* @return String
*/
public String flushDB() {
return (String) redisTemplate.execute(new RedisCallback<Object>() {
public String doInRedis(RedisConnection connection)
throws DataAccessException {
connection.flushDb();
return "ok";
}
});
}
/**
* 查询数据库使用情况
*
* @return long
*/
public long dbSize() {
return (Long) redisTemplate.execute(new RedisCallback<Object>() {
public Long doInRedis(RedisConnection connection)
throws DataAccessException {
return connection.dbSize();
}
});
}
/**
* 连接测试
*
* @return String
*/
public String ping() {
return (String) redisTemplate.execute(new RedisCallback<Object>() {
public String doInRedis(RedisConnection connection)
throws DataAccessException {
return connection.ping();
}
});
}
/**
* 匹配数据库中所有 key
*
* @param pattern
* 正则表达式
* @return Set<?>
*/
public Set<?> Setkeys(String pattern) {
return redisTemplate.keys(pattern);
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ezgo.erc.service.CacheService;
//测试类
public class testRedis {
public static void main(String[] args) {
ClassPathXmlApplicationContext context22 = new ClassPathXmlApplicationContext("config/applicationContext_core.xml");
CacheService cacheService = (CacheService) context22.getBean(CacheService.class);
String key = "name2";
if (cacheService.exists(key)) {
// cacheService.exists(key) 相当于命令 exists name2
System.out.println(cacheService.get(key));
// cacheService.get(key) 方法相当于 命令 get name2
}
String ping = cacheService.ping();
// 此方法相当于 命令 redis-cli ping
System.out.println(ping);
cacheService.set("redis_test", "redis_test");
cacheService.set("redis_test_live", "noly 30S", 300);
long size = cacheService.dbSize();
// cacheService.dbSize(); 相当于 命令 dbsize
System.out.println(size);
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
4 执行测试类 然后看到控制台 输出结果
5 如果出现连接不了redis的错误 那么修改虚拟机里面的 redis.conf 配置文件
# By default Redis listens for connections from all the network interfaces# available on the server. It is possible to listen to just one or multiple# interfaces using the "bind" configuration directive, followed by one or# more IP addresses.## Examples:## bind 192.168.1.100 10.0.0.1# bind 127.0.0.1
必须注释掉“bind 127.0.0.1”,否则只能在本机上连接Redis,远端不能连接Redis