1. Import dependence
<properties> <junit.version>4.12</junit.version> <spring.version>4.2.4.RELEASE</spring.version> </properties> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> </dependency> <!--Redis依赖--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version> </dependency> </dependencies>
2. Add the configuration file in the resources folder
redis-config.properties
= 192.168.200.128 redis.host
redis.port = 6379
#redis a verification added, no authentication is not added
redis.pass =
redis.database = 0
redis.maxIdle = 300
redis.maxWait = 3000
redis.testOnBorrow to true =
applicationContext-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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:cache="http://www.springframework.org/schema/cache" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <context:property-placeholder location="classpath*:redis-config.properties" /> <!-- redis 相关配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxWaitMillis" value="${redis.maxWait}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="JedisConnectionFactory" /> </bean> </beans>
3. Test class
@RunWith (. The SpringJUnit4ClassRunner class ) @ContextConfiguration (locations = "CLASSPATH: /spring/applicationContext-redis.xml" ) public class RedisTest { @Autowired Private RedisTemplate redisTemplate; // 1.redis simple data types @Test public void Test01 () { redisTemplate.opsForValue () SET (. "key1", "Huawei"); // add data to the redis String key1 = (String) redisTemplate.opsForValue () GET ( "key1");. // Get the redis data System.out.println (key1); redisTemplate.delete ("key1"); // delete the data in redis Object key2 = redisTemplate.opsForValue () GET ( "key1");. // get data redis in System.out.println (key2); // if no redis value corresponding return null } // 2.redis value type operation @Test public void Test02 () { redisTemplate.boundValueOps ( "Key") SET ( "Data-redis-Spring");. // added to the redis data Object Key = redisTemplate.boundValueOps ( "Key") GET ();. // Get the data redis System.out.println (Key); redisTemplate.delete("Key"); // delete the data in redis Object key1 = redisTemplate.boundValueOps ( "Key") GET ();. // get data redis in System.out.println (key1); // if no redis value corresponding return null } // 3.redis the Set operation type @Test public void TEST03 () { redisTemplate.boundSetOps ( "SET") the Add ( "Huawei." ); redisTemplate.boundSetOps ( . "SET") the Add ( "Apple" ); redisTemplate.boundSetOps ( "the SET") the Add ( "Samsung." ); redisTemplate.boundSetOps("set").add("小米"); redisTemplate.boundSetOps("set").add("vivo"); redisTemplate.boundSetOps("set").add("魅族"); Set set = redisTemplate.boundSetOps("set").members(); System.out.println(set); //redisTemplate.boundSetOps("set").remove("魅族"); redisTemplate.delete("set"); Set set1 = redisTemplate.boundSetOps("set").members(); System.out.println(set1); } //4.redis List类型操作 @Test public void test04(){ redisTemplate.boundListOps("list").leftPush("a"); //从左边添加数据 redisTemplate.boundListOps("list").leftPush("b"); redisTemplate.boundListOps("list").leftPush("c"); redisTemplate.boundListOps("list").rightPush("1"); //从右边添加数据 redisTemplate.boundListOps("list").rightPush("2"); redisTemplate.boundListOps("list").rightPush("3"); List list = redisTemplate.boundListOps("list").range(0, -1); System.out.println(list); redisTemplate.delete("list"); List list1 = redisTemplate.boundListOps("list").range(0, -1); System.out.println(list1); } //5.Hash类型操作 @Test public void test05(){ redisTemplate.boundHashOps("hash").put("name", "张三"); redisTemplate.boundHashOps("hash").put("sex", "男"); redisTemplate.boundHashOps("hash").put("age", "18"); Map map = redisTemplate.boundHashOps("hash").entries(); for (Object key : map.keySet()) { System.out.println(key +" : " + map.get(key) ); } redisTemplate.boundHashOps("hash").delete("sex"); Map map1 = redisTemplate.boundHashOps("hash").entries(); for (Object o : map1.entrySet()) { Map.Entry entry = (Map.Entry) o; System.out.println(entry.getKey()+":"+entry.getValue()); } } }
4. Use Project
Start redis query data, if it is null, it means no redis data, and then look for data from mysql database, then redis data into the cache. After the database CRUD operations must be clear redis cache to prevent data inconsistencies.
public class ContentServiceImpl implements ContentService { @Autowired private ContentMapper contentMapper; @Autowired private RedisTemplate redisTemplate; @Override public void insert(Content content) { //redis缓存 contentMapper.insertSelective(content); //清楚缓存 redisTemplate.boundHashOps(RedisConst.CONTENT).delete(content.getCategoryId()); } @Override public void delete(Long[] ids) { //清空缓存 for (Long id : ids) { CategoryId Long = contentMapper.selectByPrimaryKey (ID) .getCategoryId (); redisTemplate.boundHashOps (RedisConst.CONTENT) .Delete (categoryId); } // from the database to delete records List <Long> List = Arrays.asList (IDS); ContentExample Example = new new ContentExample (); example.createCriteria () andIdIn (List);. contentMapper.deleteByExample (Example); } @Override public void Update (the Content Content) { // query classification id before modification Long oldCategoryId =contentMapper.selectByPrimaryKey (content.getId ()) getCategoryId ();. redisTemplate.boundHashOps (RedisConst.CONTENT) .Delete (oldCategoryId); // clear the cache contentMapper.updateByPrimaryKeySelective (Content); // Classification id is modified if the modified after removal of the modified cache id classification IF (! oldCategoryId.longValue () = content.getCategoryId () longValue in ().) { redisTemplate.boundHashOps (RedisConst.CONTENT) .Delete (content.getCategoryId ()); } } / / according classified ads ads id query list @Override public list <Content> findByCategoryId ( Long categoryId) { // Redis caching mechanism //1. Data taken from redis List <the Content> Contents = (List <the Content> ) redisTemplate.boundHashOps (RedisConst.CONTENT) .get (categoryId); // if there is data in redis 2. Analyzing IF (Contents == null ) { // 3. Find mysql database data ContentExample = Example new new ContentExample (); ContentExample.Criteria Criteria = example.createCriteria (); criteria.andCategoryIdEqualTo (categoryId); criteria.andStatusEqualTo ( ". 1"); // open state example.setOrderByClause ( "the sort_order" ); Contents =contentMapper.selectByExample (Example); // 4. Save the query to the database to database Redis redisTemplate.boundHashOps (RedisConst.CONTENT) .put (categoryId, Contents); } return Contents; } }