Redis的安装过程及与SSM结合使用
Redis安装过程
- 下载安装包,解压,Mac系统将文件夹放在
/usr/local
文件夹下 - 打开终端,先安装服务器端server
终端$cd /usr/local/redis/
- 去文件下进入redis.config文件
找到requirepass删除前面的#
并修改后面拼接的密码
保存 - 进入终端
输入命令
终端$cd src/
终端$./redis-server ../redis.conf
进入redis的服务器端
Ctrl + c退出
(如果在过程中提示没有权限就在命令前加sudo)
但在使用的过程中不要退出
- 再开启一个终端,安装客户端试用
命令
终端$cd /usr/local/redis/src/
终端$./redis-cli -a
密码
就进入到了redis的客户端
操作redis客户端的命令
*
前面的是ip地址:端口号
keys * 查询所有的key
*
设置key和value
* get name
通过key获取value
* flushdb
清除redis内存中的数据,但不建议经常使用,影响服务器的性能
* del name
也可以通过key删除数据
* exit
退出
redis和SSM结合使用
1.搭建一个SSM的项目,并连接数据库
下面我主要介绍和redis有关的配置
2.创建RedisCache类,这个类是redis的核心
package com.dhl.redis;
// redis缓存
// jedis redis for Java
/**
* 序列化的是数据
* 不会序列化类结构
* 但会序列化关系(数据对应的类结构)
*/
public class RedisCache implements Cache {
// 基于Redis的MyBatis二级缓存的实现
// 需要中间类对其进行静态注
private static JedisConnectionFactory jedisConnectionFactory;
private final String id;
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {
RedisCache.jedisConnectionFactory = jedisConnectionFactory;
}
// 根据标识构造缓存对象
public RedisCache(String id) {
if (id == null){
// 抛出非法参数异常
throw new IllegalArgumentException("缓存id不能为空");
}
this.id = id;
}
/**
* 获取缓存对象的唯一标准
*
* @return
*/
public String getId() {
return this.id;
}
/**
* 把key/value保存到缓存对象中
*/
public void putObject(Object key, Object value) {
JedisConnection jedisConnection = null;
try {
//操作readis进行保存
jedisConnection = (JedisConnection) jedisConnectionFactory.getConnection();
//创建一个序列化工具
RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();
// 创建连接,保存key的value
jedisConnection.set(serializer.serialize(key),serializer.serialize(value));
}catch (JedisConnectionException e){
e.printStackTrace();
}finally {
if (jedisConnection != null){
jedisConnection.close();
}
}
}
public Object getObject(Object key) {
// 通过key从缓存对象中获取value
Object result = null;
JedisConnection jedisConnection = null;
try {
// 建立连接
jedisConnection = (JedisConnection) jedisConnectionFactory.getConnection();
RedisSerializer<Object> serializer= new JdkSerializationRedisSerializer();
byte[] bytes = jedisConnection.get(serializer.serialize(key));
// 1. 将key序列化
// 2. 通过connection获取序列化的value
// 3. 将序列化的value反序列化
result = serializer.deserialize(bytes);
/**
* 序列化的是数据
* 不会序列化类结构
* 但会序列化关系(数据对应的类结构)
*/
}catch (JedisConnectionException e){
e.printStackTrace();
}finally {
if (jedisConnection != null){
jedisConnection.close();
}
}
return result;
}
/**
* 不是需要实现的方法,没有被核心框架调用
* 方法作用: 根据key移除对应的value
*/
public Object removeObject(Object key) {
Object result = null;
JedisConnection jedisConnection = null;
try {
// 建立连接
jedisConnection = (JedisConnection) jedisConnectionFactory.getConnection();
RedisSerializer<Object> serializer= new JdkSerializationRedisSerializer();
// 移除保存到的值
result = jedisConnection.expire(serializer.serialize(key),0);
}catch (JedisConnectionException e){
e.printStackTrace();
}finally {
if (jedisConnection != null){
jedisConnection.close();
}
}
return result;
}
// 清除缓存(操作影响性能,效率低,不建议经常清除)
public void clear() {
JedisConnection jedisConnection = null;
try {
jedisConnection = (JedisConnection) jedisConnectionFactory.getConnection();
// 清除缓存
jedisConnection.flushDb();
// 清除设置
jedisConnection.flushAll();
}catch (JedisConnectionException e){
}finally {
if (jedisConnection != null){
jedisConnection.close();
}
}
}
/**
* 获取缓存对象中存取的key/value的数量
* @return
*/
public int getSize() {
Integer count = 0;
JedisConnection jedisConnection = null;
try {
jedisConnection = (JedisConnection) jedisConnectionFactory.getConnection();
count = Integer.valueOf(jedisConnection.dbSize().toString());
}catch (JedisConnectionException e){
}finally {
if (jedisConnection != null){
jedisConnection.close();
}
}
return count;
}
// 获取读写锁
// 从mybatis的3.2.6版本,这个方法不再被框架核心调用
// 所有需要的锁,都必须由缓存供应商来提供
public ReadWriteLock getReadWriteLock() {
return this.readWriteLock;
}
}
3.创建中间类
package com.lanou.redis;
public class RedisCacheTransfer {
@Autowired
public void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory){
RedisCache.setJedisConnectionFactory(jedisConnectionFactory);
}
}
4.redis的配置文件—-(1)redis.properties
# Redis的配置
redis.host=127.0.0.1
redis.port=6379
redis.pass=111111
redis.maxIdl=300
redis.maxActive=600
redis.maxWait=1000
5.redis的配置文件—-(2)SSM-redis.xml
这个配置文件也可以和SSM-mybatis.xml配置文件写在一起
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:redis.properties</value>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
<!--构建redis的数据源-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdl}"/>
<property name="maxTotal" value="${redis.maxActive}"/>
<property name="maxWaitMillis" value="${redis.maxWait}"/>
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:hostName="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:poolConfig-ref="poolConfig"
/>
<bean id="redisCacheTranfer" class="com.lanou.redis.RedisCacheTransfer">
<property name="jedisConnectionFactory" ref="jedisConnectionFactory"/>
</bean>
</beans>
6.mybatis的配置文件—–mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- Globally enables or disables any caches configured in any mapper under this configuration -->
<setting name="cacheEnabled" value="true"/>
<!-- Sets the number of seconds the driver will wait for a response from the database -->
<setting name="defaultStatementTimeout" value="3000"/>
<!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- Allows JDBC support for generated keys. A compatible driver is required.
This setting forces generated keys to be used if set to true,
as some drivers deny compatibility but still work -->
<setting name="useGeneratedKeys" value="true"/>
<!--查询的时候,关闭关联对象即使加载,为了提高性能-->
<setting name="lazyLoadingEnabled" value="false"/>
<!--允许多个结果集,默认就是true-->
<setting name="multipleResultSetsEnabled" value="true"/>
<!--设置关联的对象加载的形态,此处是按需加载字段.不会加载所有的字段,而是有sql来决定-->
<setting name="aggressiveLazyLoading" value="true"/>
</settings>
<!-- Continue going here -->
</configuration>
7.最后不要忘记在web.xml的配置文件中配置SSM-redis.xml
8.可以写查询语句进行测试,运行成功之后我们可以去进入终端,使用redis的客户端,通过key * 可以查到内存中的数据
Redis安装过程
- 下载安装包,解压,Mac系统将文件夹放在
/usr/local
文件夹下 - 打开终端,先安装服务器端server
终端$cd /usr/local/redis/
- 去文件下进入redis.config文件
找到requirepass删除前面的#
并修改后面拼接的密码
保存 - 进入终端
输入命令
终端$cd src/
终端$./redis-server ../redis.conf
进入redis的服务器端
Ctrl + c退出
(如果在过程中提示没有权限就在命令前加sudo)
但在使用的过程中不要退出
- 再开启一个终端,安装客户端试用
命令
终端 ./redis-cli -a 密码
就进入到了redis的客户端
操作redis客户端的命令
*
前面的是ip地址:端口号
keys * 查询所有的key
*
设置key和value
*
通过key获取value
*
清除redis内存中的数据,但不建议经常使用,影响服务器的性能
*
也可以通过key删除数据
*
退出
redis和SSM结合使用
1.搭建一个SSM的项目,并连接数据库
下面我主要介绍和redis有关的配置
2.创建RedisCache类,这个类是redis的核心
package com.dhl.redis;
// redis缓存
// jedis redis for Java
/**
* 序列化的是数据
* 不会序列化类结构
* 但会序列化关系(数据对应的类结构)
*/
public class RedisCache implements Cache {
// 基于Redis的MyBatis二级缓存的实现
// 需要中间类对其进行静态注
private static JedisConnectionFactory jedisConnectionFactory;
private final String id;
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {
RedisCache.jedisConnectionFactory = jedisConnectionFactory;
}
// 根据标识构造缓存对象
public RedisCache(String id) {
if (id == null){
// 抛出非法参数异常
throw new IllegalArgumentException("缓存id不能为空");
}
this.id = id;
}
/**
* 获取缓存对象的唯一标准
*
* @return
*/
public String getId() {
return this.id;
}
/**
* 把key/value保存到缓存对象中
*/
public void putObject(Object key, Object value) {
JedisConnection jedisConnection = null;
try {
//操作readis进行保存
jedisConnection = (JedisConnection) jedisConnectionFactory.getConnection();
//创建一个序列化工具
RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();
// 创建连接,保存key的value
jedisConnection.set(serializer.serialize(key),serializer.serialize(value));
}catch (JedisConnectionException e){
e.printStackTrace();
}finally {
if (jedisConnection != null){
jedisConnection.close();
}
}
}
public Object getObject(Object key) {
// 通过key从缓存对象中获取value
Object result = null;
JedisConnection jedisConnection = null;
try {
// 建立连接
jedisConnection = (JedisConnection) jedisConnectionFactory.getConnection();
RedisSerializer<Object> serializer= new JdkSerializationRedisSerializer();
byte[] bytes = jedisConnection.get(serializer.serialize(key));
// 1. 将key序列化
// 2. 通过connection获取序列化的value
// 3. 将序列化的value反序列化
result = serializer.deserialize(bytes);
/**
* 序列化的是数据
* 不会序列化类结构
* 但会序列化关系(数据对应的类结构)
*/
}catch (JedisConnectionException e){
e.printStackTrace();
}finally {
if (jedisConnection != null){
jedisConnection.close();
}
}
return result;
}
/**
* 不是需要实现的方法,没有被核心框架调用
* 方法作用: 根据key移除对应的value
*/
public Object removeObject(Object key) {
Object result = null;
JedisConnection jedisConnection = null;
try {
// 建立连接
jedisConnection = (JedisConnection) jedisConnectionFactory.getConnection();
RedisSerializer<Object> serializer= new JdkSerializationRedisSerializer();
// 移除保存到的值
result = jedisConnection.expire(serializer.serialize(key),0);
}catch (JedisConnectionException e){
e.printStackTrace();
}finally {
if (jedisConnection != null){
jedisConnection.close();
}
}
return result;
}
// 清除缓存(操作影响性能,效率低,不建议经常清除)
public void clear() {
JedisConnection jedisConnection = null;
try {
jedisConnection = (JedisConnection) jedisConnectionFactory.getConnection();
// 清除缓存
jedisConnection.flushDb();
// 清除设置
jedisConnection.flushAll();
}catch (JedisConnectionException e){
}finally {
if (jedisConnection != null){
jedisConnection.close();
}
}
}
/**
* 获取缓存对象中存取的key/value的数量
* @return
*/
public int getSize() {
Integer count = 0;
JedisConnection jedisConnection = null;
try {
jedisConnection = (JedisConnection) jedisConnectionFactory.getConnection();
count = Integer.valueOf(jedisConnection.dbSize().toString());
}catch (JedisConnectionException e){
}finally {
if (jedisConnection != null){
jedisConnection.close();
}
}
return count;
}
// 获取读写锁
// 从mybatis的3.2.6版本,这个方法不再被框架核心调用
// 所有需要的锁,都必须由缓存供应商来提供
public ReadWriteLock getReadWriteLock() {
return this.readWriteLock;
}
}
3.创建中间类
package com.lanou.redis;
public class RedisCacheTransfer {
@Autowired
public void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory){
RedisCache.setJedisConnectionFactory(jedisConnectionFactory);
}
}
4.redis的配置文件—-(1)redis.properties
# Redis的配置
redis.host=127.0.0.1
redis.port=6379
redis.pass=111111
redis.maxIdl=300
redis.maxActive=600
redis.maxWait=1000
5.redis的配置文件—-(2)SSM-redis.xml
这个配置文件也可以和SSM-mybatis.xml配置文件写在一起
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:redis.properties</value>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
<!--构建redis的数据源-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdl}"/>
<property name="maxTotal" value="${redis.maxActive}"/>
<property name="maxWaitMillis" value="${redis.maxWait}"/>
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:hostName="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:poolConfig-ref="poolConfig"
/>
<bean id="redisCacheTranfer" class="com.lanou.redis.RedisCacheTransfer">
<property name="jedisConnectionFactory" ref="jedisConnectionFactory"/>
</bean>
</beans>
6.mybatis的配置文件—–mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- Globally enables or disables any caches configured in any mapper under this configuration -->
<setting name="cacheEnabled" value="true"/>
<!-- Sets the number of seconds the driver will wait for a response from the database -->
<setting name="defaultStatementTimeout" value="3000"/>
<!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- Allows JDBC support for generated keys. A compatible driver is required.
This setting forces generated keys to be used if set to true,
as some drivers deny compatibility but still work -->
<setting name="useGeneratedKeys" value="true"/>
<!--查询的时候,关闭关联对象即使加载,为了提高性能-->
<setting name="lazyLoadingEnabled" value="false"/>
<!--允许多个结果集,默认就是true-->
<setting name="multipleResultSetsEnabled" value="true"/>
<!--设置关联的对象加载的形态,此处是按需加载字段.不会加载所有的字段,而是有sql来决定-->
<setting name="aggressiveLazyLoading" value="true"/>
</settings>
<!-- Continue going here -->
</configuration>
7.最后不要忘记在web.xml的配置文件中配置SSM-redis.xml
8.可以写查询语句进行测试,运行成功之后我们可以去进入终端,使用redis的客户端,通过key * 可以查到内存中的数据