欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springmvc集成使用redis過程

 更新時(shí)間:2021年11月11日 11:04:35   作者:蝴蝶Maple  
這篇文章主要介紹了springmvc集成使用redis過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Redis安裝

首先安裝redis。這個(gè)就不重點(diǎn)介紹了。windos下載redis就行。

我用的是mac

用命令行安裝的。

安裝命令

yum install redis 

運(yùn)行命令

sudo redis-server

這樣就安裝運(yùn)行成功了。

spring集成redis

首先你需要下載驅(qū)動(dòng)包,下載 jedis.jar,確保下載最新驅(qū)動(dòng)包。然后導(dǎo)包。

在spring配置文件里我這是ApplicationContext .xml文件添加

<!-- 引入同文件夾下的redis屬性配置文件 -->
 <import resource="spring-redis.xml" />

然后用創(chuàng)建spring-redis.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">
        
	<!-- 緩存的層級(jí)-->
 	<context:component-scan base-package="com.niit.cache" />
	<!-- 引入redis配置 -->
 	<context:property-placeholder location="/WEB-INF/classes/redis.properties" ignore-unresolvable="true"/>
 
	<!-- Redis 配置 -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="${redis.pool.maxTotal}" />
		<property name="maxIdle" value="${redis.pool.maxIdle}" />
		<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
		<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
	</bean>
 
	<!-- JedisCluster 集群高可用配置 -->
	<!--<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
		<constructor-arg index="0">
			<set>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg index="0" value="${redis.ip1}" />
					<constructor-arg index="1" value="${redis.port1}" type="int" />
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg index="0" value="${redis.ip2}" />
					<constructor-arg index="1" value="${redis.port2}" type="int" />
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg index="0" value="${redis.ip3}" />
					<constructor-arg index="1" value="${redis.port3}" type="int" />
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg index="0" value="${redis.ip4}" />
					<constructor-arg index="1" value="${redis.port4}" type="int" />
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg index="0" value="${redis.ip5}" />
					<constructor-arg index="1" value="${redis.port5}" type="int" />
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg index="0" value="${redis.ip6}" />
					<constructor-arg index="1" value="${redis.port6}" type="int" />
				</bean>
			</set>
		</constructor-arg>
		<constructor-arg index="1" value="2000" type="int"></constructor-arg>
		<constructor-arg index="2" value="100" type="int"></constructor-arg>
		<constructor-arg index="3" ref="jedisPoolConfig"></constructor-arg>
	</bean>-->
 
	<!--redis Sentinel主從高可用方案配置 -->
	<!-- <bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
		<property name="master">
			<bean class="org.springframework.data.redis.connection.RedisNode">
				<property name="name" value="master-1"></property>
			</bean>
		</property>
		<property name="sentinels">
			<set>
				<bean class="org.springframework.data.redis.connection.RedisNode">
					<constructor-arg name="host" value="${sentinel1.ip}"></constructor-arg>
					<constructor-arg name="port" value="${sentinel1.port}"></constructor-arg>
				</bean>
				<bean class="org.springframework.data.redis.connection.RedisNode">
					<constructor-arg name="host" value="${sentinel2.ip}"></constructor-arg>
					<constructor-arg name="port" value="${sentinel2.port}"></constructor-arg>
				</bean>
				<bean class="org.springframework.data.redis.connection.RedisNode">
					<constructor-arg name="host" value="${sentinel3.ip}"></constructor-arg>
					<constructor-arg name="port" value="${sentinel3.port}"></constructor-arg>
				</bean>
			</set>
		</property>
	</bean>
	<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true">
		<property name="password" value="${redis.pass}" />
		<property name="poolConfig">
			<ref bean="jedisPoolConfig" />
		</property>
		<constructor-arg name="sentinelConfig" ref="sentinelConfiguration" />
	</bean> -->
 
	<!-- redis單節(jié)點(diǎn)數(shù)據(jù)庫連接配置 -->
	<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="hostName" value="${redis.ip}" />
		<property name="port" value="${redis.port}" />
		<property name="password" value="${redis.pass}" />
		<property name="poolConfig" ref="jedisPoolConfig" />
	</bean> 
 
	<!-- redisTemplate配置,redisTemplate是對(duì)Jedis的對(duì)redis操作的擴(kuò)展,有更多的操作,封裝使操作更便捷 -->
	<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
		<property name="connectionFactory" ref="jedisConnectionFactory" />
	</bean>
	
</beans>
public class RedisCache { 
	public final static String CAHCENAME = "niitcache";// 緩存名
	public final static int CAHCETIME = 60;// 默認(rèn)緩存時(shí)間 60S
	public final static int CAHCEHOUR = 60 * 60;// 默認(rèn)緩存時(shí)間 1hr
	public final static int CAHCEDAY = 60 * 60 * 24;// 默認(rèn)緩存時(shí)間 1Day
	public final static int CAHCEWEEK = 60 * 60 * 24 * 7;// 默認(rèn)緩存時(shí)間 1week
	public final static int CAHCEMONTH = 60 * 60 * 24 * 7 * 30;// 默認(rèn)緩存時(shí)間 1month
 
	@Autowired
	private RedisTemplate<String, String> redisTemplate; 
	public <T> boolean putCache(String key, T obj) {
		final byte[] bkey = key.getBytes();
		final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj);
		boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				return connection.setNX(bkey, bvalue);
			}
		});
		return result;
	}
 
	public <T> void putCacheWithExpireTime(String key, T obj, final long expireTime) {
		final byte[] bkey = key.getBytes();
		final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj);
		redisTemplate.execute(new RedisCallback<Boolean>() {
			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				connection.setEx(bkey, expireTime, bvalue);
				return true;
			}
		});
	}
 
	public <T> boolean putListCache(String key, List<T> objList) {
		final byte[] bkey = key.getBytes();
		final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList);
		boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				return connection.setNX(bkey, bvalue);
			}
		});
		return result;
	}
 
	public <T> boolean putListCacheWithExpireTime(String key, List<T> objList, final long expireTime) {
		final byte[] bkey = key.getBytes();
		final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList);
		boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				connection.setEx(bkey, expireTime, bvalue);
				return true;
			}
		});
		return result;
	}
 
	public <T> T getCache(final String key, Class<T> targetClass) {
		byte[] result = redisTemplate.execute(new RedisCallback<byte[]>() {
			@Override
			public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
				return connection.get(key.getBytes());
			}
		});
		if (result == null) {
			return null;
		}
		return ProtoStuffSerializerUtil.deserialize(result, targetClass);
	}
 
	public <T> List<T> getListCache(final String key, Class<T> targetClass) {
		byte[] result = redisTemplate.execute(new RedisCallback<byte[]>() {
			@Override
			public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
				return connection.get(key.getBytes());
			}
		});
		if (result == null) {
			return null;
		}
		return ProtoStuffSerializerUtil.deserializeList(result, targetClass);
	}
 
	/**
	 * 精確刪除key
	 * 
	 * @param key
	 */
	public void deleteCache(String key) {
		redisTemplate.delete(key);
	}
 
	/**
	 * 模糊刪除key
	 * 
	 * @param pattern
	 */
	public void deleteCacheWithPattern(String pattern) {
		Set<String> keys = redisTemplate.keys(pattern);
		redisTemplate.delete(keys);
	}
 
	/**
	 * 清空所有緩存
	 */
	public void clearCache() {
		deleteCacheWithPattern(RedisCache.CAHCENAME + "|*");
	}
}

創(chuàng)建redis的配置文件 redis.properties。

寫入

#redis config
redis.pass=
redis.pool.maxTotal=105
redis.pool.maxIdle=10
redis.pool.maxWaitMillis=5000
redis.pool.testOnBorrow=true
 
redis.ip=127.0.0.1
redis.port=6379

這些根據(jù)自己的需求自定義配置就好了

這樣redis就繼承好了

SpringMVC中使用redis

創(chuàng)建一個(gè)redisCache類

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.Component; 
import com.niit.util.ProtoStuffSerializerUtil; 
import java.util.List;
import java.util.Set;
 
/**
 * redis緩存
 * 
 * @author James
 *
 */
@Component
public class RedisCache {
 
	public final static String CAHCENAME = "niitcache";// 緩存名
	public final static int CAHCETIME = 60;// 默認(rèn)緩存時(shí)間 60S
	public final static int CAHCEHOUR = 60 * 60;// 默認(rèn)緩存時(shí)間 1hr
	public final static int CAHCEDAY = 60 * 60 * 24;// 默認(rèn)緩存時(shí)間 1Day
	public final static int CAHCEWEEK = 60 * 60 * 24 * 7;// 默認(rèn)緩存時(shí)間 1week
	public final static int CAHCEMONTH = 60 * 60 * 24 * 7 * 30;// 默認(rèn)緩存時(shí)間 1month
 
	@Autowired
	private RedisTemplate<String, String> redisTemplate; 
	public <T> boolean putCache(String key, T obj) {
		final byte[] bkey = key.getBytes();
		final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj);
		boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				return connection.setNX(bkey, bvalue);
			}
		});
		return result;
	}
 
	public <T> void putCacheWithExpireTime(String key, T obj, final long expireTime) {
		final byte[] bkey = key.getBytes();
		final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj);
		redisTemplate.execute(new RedisCallback<Boolean>() {
			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				connection.setEx(bkey, expireTime, bvalue);
				return true;
			}
		});
	}
 
	public <T> boolean putListCache(String key, List<T> objList) {
		final byte[] bkey = key.getBytes();
		final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList);
		boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				return connection.setNX(bkey, bvalue);
			}
		});
		return result;
	}
 
	public <T> boolean putListCacheWithExpireTime(String key, List<T> objList, final long expireTime) {
		final byte[] bkey = key.getBytes();
		final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList);
		boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				connection.setEx(bkey, expireTime, bvalue);
				return true;
			}
		});
		return result;
	}
 
	public <T> T getCache(final String key, Class<T> targetClass) {
		byte[] result = redisTemplate.execute(new RedisCallback<byte[]>() {
			@Override
			public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
				return connection.get(key.getBytes());
			}
		});
		if (result == null) {
			return null;
		}
		return ProtoStuffSerializerUtil.deserialize(result, targetClass);
	}
 
	public <T> List<T> getListCache(final String key, Class<T> targetClass) {
		byte[] result = redisTemplate.execute(new RedisCallback<byte[]>() {
			@Override
			public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
				return connection.get(key.getBytes());
			}
		});
		if (result == null) {
			return null;
		}
		return ProtoStuffSerializerUtil.deserializeList(result, targetClass);
	}
 
	/**
	 * 精確刪除key
	 * 
	 * @param key
	 */
	public void deleteCache(String key) {
		redisTemplate.delete(key);
	}
 
	/**
	 * 模糊刪除key
	 * 
	 * @param pattern
	 */
	public void deleteCacheWithPattern(String pattern) {
		Set<String> keys = redisTemplate.keys(pattern);
		redisTemplate.delete(keys);
	}
 
	/**
	 * 清空所有緩存
	 */
	public void clearCache() {
		deleteCacheWithPattern(RedisCache.CAHCENAME + "|*");
	}
}

寫進(jìn)和讀取redis

<span style="white-space:pre">  </span>String v = "test";
  cache.putCacheWithExpireTime("key", v, cache.CAHCEHOUR);
  String value = cache.getCache("key", String.class);
  System.out.println(value);

然后集成成功。redis是將數(shù)據(jù)放進(jìn)內(nèi)存里。所以需要考慮做redis服務(wù)器時(shí)候的內(nèi)存性能,還有redis的緩存策略等等。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Redis中Hash類型的使用

    Redis中Hash類型的使用

    本文主要介紹了Redis中Hash類型的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 了解redis中RDB結(jié)構(gòu)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    了解redis中RDB結(jié)構(gòu)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了redis中RDB結(jié)構(gòu),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 比較幾種Redis集群方案

    比較幾種Redis集群方案

    Redis高可用集群是一個(gè)由多個(gè)主從節(jié)點(diǎn)群組成的分布式服務(wù)器群,它具有復(fù)制、高可用和分片特性。Redis集群不需要sentinel哨兵也能完成節(jié)點(diǎn)移除和故障轉(zhuǎn)移的功能,只要將每個(gè)節(jié)點(diǎn)設(shè)置成集群模式,這種集群模式?jīng)]有中心節(jié)點(diǎn),可水平擴(kuò)展,官方稱可以線性擴(kuò)展到上萬個(gè)節(jié)點(diǎn)
    2021-06-06
  • redis集群實(shí)現(xiàn)清理前綴相同的key

    redis集群實(shí)現(xiàn)清理前綴相同的key

    這篇文章主要介紹了redis集群實(shí)現(xiàn)清理前綴相同的key,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Redis實(shí)現(xiàn)用戶關(guān)注的項(xiàng)目實(shí)踐

    Redis實(shí)現(xiàn)用戶關(guān)注的項(xiàng)目實(shí)踐

    本文主要介紹了Redis實(shí)現(xiàn)用戶關(guān)注的項(xiàng)目實(shí)踐,通過使用Redis的set數(shù)據(jù)結(jié)構(gòu)來存儲(chǔ)關(guān)注對(duì)象,方便高效地進(jìn)行添加和取消關(guān)注操作,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • SpringSession通過Redis統(tǒng)計(jì)在線用戶數(shù)量的實(shí)現(xiàn)代碼

    SpringSession通過Redis統(tǒng)計(jì)在線用戶數(shù)量的實(shí)現(xiàn)代碼

    這篇文章主要介紹了SpringSession通過Redis統(tǒng)計(jì)在線用戶數(shù)量,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • redis數(shù)據(jù)類型及應(yīng)用場(chǎng)景知識(shí)點(diǎn)總結(jié)

    redis數(shù)據(jù)類型及應(yīng)用場(chǎng)景知識(shí)點(diǎn)總結(jié)

    在本篇文章里小編給大家整理的是關(guān)于
    2020-02-02
  • Redis 徹底禁用RDB持久化操作

    Redis 徹底禁用RDB持久化操作

    這篇文章主要介紹了Redis 徹底禁用RDB持久化的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Redis排序命令Sort深入解析

    Redis排序命令Sort深入解析

    這篇文章主要為大家介紹了Redis排序命令Sort深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • redis與mongodb的區(qū)別總結(jié)

    redis與mongodb的區(qū)別總結(jié)

    在本篇文章里小編給大家分享的是關(guān)于redis與mongodb的區(qū)別的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們參考下。
    2019-06-06

最新評(píng)論