最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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加鎖的幾種方式匯總

    redis加鎖的幾種方式匯總

    這篇文章主要介紹了redis加鎖的幾種方式匯總,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Redis實(shí)現(xiàn)延遲任務(wù)的三種方法詳解

    Redis實(shí)現(xiàn)延遲任務(wù)的三種方法詳解

    延遲任務(wù)(Delayed Task)是指在未來的某個(gè)時(shí)間點(diǎn),執(zhí)行相應(yīng)的任務(wù),本文為大家整理了三種常見的實(shí)現(xiàn)方法,感興趣的小伙伴可以參考一下
    2025-04-04
  • Redis解決跨域存取Session問題

    Redis解決跨域存取Session問題

    本文主要介紹了Redis解決跨域存取Session問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Redis高效查詢大數(shù)據(jù)的實(shí)踐與優(yōu)化詳細(xì)指南

    Redis高效查詢大數(shù)據(jù)的實(shí)踐與優(yōu)化詳細(xì)指南

    Redis 是一種高性能的鍵值存儲(chǔ)數(shù)據(jù)庫,廣泛應(yīng)用于緩存,排行榜,計(jì)數(shù)器等場(chǎng)景,本文將圍繞如何高效查詢Redis中滿足條件的數(shù)據(jù)展開討論,感興趣的小伙伴可以了解下
    2025-04-04
  • 深入理解Redis內(nèi)存淘汰策略

    深入理解Redis內(nèi)存淘汰策略

    本文主要介紹了深入理解Redis內(nèi)存淘汰策略,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • redis5.0以上基于密碼認(rèn)證的集群cluster方式

    redis5.0以上基于密碼認(rèn)證的集群cluster方式

    這篇文章主要介紹了redis5.0以上基于密碼認(rèn)證的集群cluster方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 使用Redis實(shí)現(xiàn)實(shí)時(shí)排行榜功能

    使用Redis實(shí)現(xiàn)實(shí)時(shí)排行榜功能

    排行榜功能是一個(gè)很普遍的需求。使用 Redis 中有序集合的特性來實(shí)現(xiàn)排行榜是又好又快的選擇。接下來通過本文給大家介紹使用Redis實(shí)現(xiàn)實(shí)時(shí)排行榜功能,需要的朋友可以參考下
    2021-07-07
  • 基于redis實(shí)現(xiàn)定時(shí)任務(wù)的方法詳解

    基于redis實(shí)現(xiàn)定時(shí)任務(wù)的方法詳解

    這篇文章主要給大家介紹了基于redis實(shí)現(xiàn)定時(shí)任務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用redis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • springboot中操作redis實(shí)例分享

    springboot中操作redis實(shí)例分享

    本文介紹了如何在Spring?Boot應(yīng)用中整合Redis緩存技術(shù),包括配置Redis連接、定義Redis模板、實(shí)現(xiàn)Redis的基本操作以及使用Spring?Cache注解。這些內(nèi)容可幫助開發(fā)者快速掌握Spring?Boot與Redis的集成,并提高應(yīng)用性能。
    2023-06-06
  • Redis特殊數(shù)據(jù)類型HyperLogLog基數(shù)統(tǒng)計(jì)算法講解

    Redis特殊數(shù)據(jù)類型HyperLogLog基數(shù)統(tǒng)計(jì)算法講解

    這篇文章主要為大家介紹了Redis特殊數(shù)據(jù)類型HyperLogLog基數(shù)統(tǒng)計(jì)算法講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05

最新評(píng)論

蒲城县| 三门县| 上饶县| 鄂尔多斯市| 彰化县| 青浦区| 安达市| 遂川县| 黄梅县| 清远市| 南康市| 定安县| 民县| 潍坊市| 娄底市| 金乡县| 凤冈县| 公安县| 福清市| 双城市| 嘉义县| 子长县| 胶州市| 淮南市| 卢龙县| 景泰县| 兴宁市| 永清县| 雷州市| 唐海县| 嘉祥县| 昌江| 杭州市| 湄潭县| 昌平区| 威信县| 香河县| 安远县| 防城港市| 逊克县| 蕲春县|