Jedis+redis+spring Cache

Source: Internet
Author: User
Tags redis server

Does the Redis program use it?
Jedis accessing the Redis Java API

Redis-server &//Background operation
Firewall to shut down


Ts-parent's Pom.xml plus Jedis dependency
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.0</version>
</dependency>

Writing test Classes
Package Jedis;

Import Org.junit.Test;

Import Redis.clients.jedis.Jedis;

public class Testjedis {
@Test
public void Jedis () {
Connect to a Redis server, Ip+port
String IP = "192.168.27.113";
Get to Jedis Object
Jedis Jedis = new Jedis (IP, 6379);
Call Redis Set,key=name,value=tony
Jedis.set ("name", "Tony");
System.out.println (Jedis.get ("name"));
}
}

Package Jedis;

Import java.util.ArrayList;
Import java.util.List;

Import Org.junit.Test;

Import Redis.clients.jedis.Jedis;
Import Redis.clients.jedis.JedisPoolConfig;
Import Redis.clients.jedis.JedisShardInfo;
Import Redis.clients.jedis.ShardedJedis;
Import Redis.clients.jedis.ShardedJedisPool;

public class Testjedis {
@Test
public void Jedis () {
Connect to a Redis server, Ip+port
String IP = "192.168.27.113";
Get to Jedis Object
Jedis Jedis = new Jedis (IP, 6379);
Call Redis Set,key=name,value=tony
Jedis.set ("name", "Tony");
System.out.println (Jedis.get ("name"));
}

@Test//Shard shard Pooling
public void sharded () {
Create a sharded Pool configuration object
Jedispoolconfig config = new Jedispoolconfig ();
Maximum number of connections
Config.setmaxtotal (50);

Connect multiple Redis nodes, IP, and port information
list<jedisshardinfo> shards = new arraylist<jedisshardinfo> ();
One of the node information is OK
Jedisshardinfo Info1 =
New Jedisshardinfo ("192.168.27.113", 6379);
Shards.add (INFO1);

Create a shard Pool
Shardedjedispool pool =
New Shardedjedispool (config, shards);

Get a Jedis link from the pool
Shardedjedis Jedis = Pool.getresource ();
System.out.println (Jedis.get ("name"));
}
}


Integrating the Jedis and spring frameworks
The Jedis object creation is given to the spring framework through an XML configuration.

Where do I use the cache?
Package cn.tedu.store.service;

Import java.io.IOException;
Import java.util.List;

Import Javax.annotation.Resource;

Import Org.springframework.stereotype.Service;

Import com.fasterxml.jackson.core.JsonProcessingException;
Import Com.fasterxml.jackson.databind.JsonNode;
Import Com.fasterxml.jackson.databind.ObjectMapper;

Import Cn.tedu.store.bean.dict.Area;
Import cn.tedu.store.bean.dict.City;
Import cn.tedu.store.bean.dict.Province;
Import Cn.tedu.store.mapper.DictMapper;
Import Redis.clients.jedis.ShardedJedis;
Import Redis.clients.jedis.ShardedJedisPool;

@Service ("Dictservice")
public class Dictserviceimpl implements Dictservice {

@Resource
Private Dictmapper Dictmapper;
Get the Jedis object created by the spring framework
@Resource
Private Shardedjedispool Shardedjedispool;
Convert objects to JSON or JSON to Java object tool classes
private static final Objectmapper MAPPER = new Objectmapper ();

Public list<province> getprovincelist () {
return Dictmapper.getprovincelist ();
}

Public list<city> getcitylist (String provincecode) {
List<city> citylist = null;
Rules for setting up Redis keys
String KEY = "Ts_city_" +provincecode;

Get the Jedis object from the pool
Shardedjedis Jedis = Shardedjedispool.getresource ();

1. Get data from Redis if data is returned directly
Note Set settings are overwritten 2 times
if (jedis.exists (KEY)) {//determine if KEY exists
String json = Jedis.get (KEY);
Jsonnode Jsonnode;
try {
Get the data from the cache, then turn the JSON into a Java object and return directly without accessing the database
Jsonnode = Mapper.readtree (JSON);
Object obj = Mapper.readvalue (Jsonnode.traverse (),
Mapper.gettypefactory (). Constructcollectiontype (List.class, City.class));
Return (list<city>) obj; Reduce database access pressure
} catch (Exception e) {
E.printstacktrace ();
}
}else{
Traditional way to read data directly from a database
CityList = Dictmapper.getcitylist (Provincecode);

2. On first access, Redis has no value and must be fetched from the database to put the value into Redis
Turns the Java list collection into a string JSON
try {
String json = mapper.writevalueasstring (citylist);
Jedis.set (KEY, JSON);
return citylist;
} catch (Exception e) {
E.printstacktrace ();
}
}
return null;
}

Public list<area> getarealist (String citycode) {
Return Dictmapper.getarealist (Citycode);
}

public string Getprovincenamebycode (string provincecode) {
Return Dictmapper.getprovincenamebycode (Provincecode);
}

public string Getcitynamebycode (string citycode) {
Return Dictmapper.getcitynamebycode (Citycode);
}

public string Getareanamebycode (string areacode) {
Return Dictmapper.getareanamebycode (AreaCode);
}

}

The Redis cache app Note points:
1, is not all the data can be placed in the cache?
Reids can replace MySQL
Of course not
Cache is the data that stores hotspot data and does not change frequently!!
What's wrong with memory power-down?
Data loss. Redis automatically disks C + + every second. MemCache

Jedis+redis+spring Cache

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.