十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
在项目中,缓存以及mq消息队列可以说是不可或缺的2个重要技术。前者主要是为了减轻数据库压力,大幅度提升性能。后者主要是为了提高用户的体验度,我理解的是再后端做的一个ajax请求(异步),并且像ribbmitmq等消息队列有重试机制等功能。
创新互联公司服务项目包括惠城网站建设、惠城网站制作、惠城网页制作以及惠城网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,惠城网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到惠城省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
这里主要讲redis如何把对象,集合存入,并且取出.
private Jedis jedis;private JedisPoolConfig config;private JedisShardInfo sharInfo;@Beanpublic Jedis jedis(){//连接redis服务器,192.168.0.100:6379// jedis = new Jedis("192.168.0.100", 6379);// //权限认证// jedis.auth("123456");// 操作单独的文本串config = new JedisPoolConfig();
config.setMaxIdle(1000);//最大空闲时间config.setMaxWaitMillis(1000); //最大等待时间config.setMaxTotal(500); //redis池中最大对象个数sharInfo = new JedisShardInfo("192.168.0.100", 6379);
sharInfo.setPassword("123456");
sharInfo.setConnectionTimeout(5000);//链接超时时间jedis = new Jedis(sharInfo);return jedis;
}
spring:
redis:
database: 0
host: 101.132.191.77
port: 6379
password: 123456
pool:
max-idle: 8 #连接池最大连接数(使用负值表示没有限制)
min-idle: 0 # 连接池中的最小空闲连接
max-active: 8 # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1 # 连接池中的最大空闲连接
timeout: 5000 # 连接超时时间(毫秒)
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;/**
public class SerializeUtil
{
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {// 序列化baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (Exception e) {
}return null;
}
public static Object unserialize( byte[] bytes) {
ByteArrayInputStream bais = null;
try {
// 反序列化bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
}return null;
}
}
redisServiceImpl.set主要是传object,setStr主要设置string
import com.ys.util.redis.SerializeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
public class RedisServiceImpl
{@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private Jedis jedis;
public void setStr(String key, String value) {
setStr(key, value, null);
}
public void setStr(String key, Object value, Long time)
{if(value == null){
return;
}if(value instanceof String){
String obj = (String) value;
stringRedisTemplate.opsForValue().set(key, obj);
}else if(value instanceof List){
List obj = (List) value;
stringRedisTemplate.opsForList().leftPushAll(key,obj);
}else if(value instanceof Map){
Map obj = (Map) value;
stringRedisTemplate.opsForHash().putAll(key,obj);
}if (time != null)
stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
}
public Object getKey(String key)
{return stringRedisTemplate.opsForValue().get(key);
}
public void delKey(String key) {
stringRedisTemplate.delete(key);
}
public boolean del(String key)
{return jedis.del(key.getBytes())>0;
}
}
import com.ys.service.impl.RedisServiceImpl;
import com.ys.vo.IqProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RestController
public class RedisServiceController
{
@Autowired
private RedisServiceImpl redisService;
@RequestMapping(value = "/setredis")
public String setredis(String keyredis){
redisService.setStr(keyredis,"2018年1月26日");
return "保存成功,请访问getredis查询redis";
}
@RequestMapping(value = "/setObj")
public String setObj(String keyredis){
IqProduct iqProduct = new IqProduct();
iqProduct.setSort(1);
iqProduct.setTimestamp(new Date().getTime());
iqProduct.setProductName("productname");
// list.add(iqProduct);
redisService.set(keyredis, iqProduct);
return "保存成功,请访问getredis查询redis";
}
@RequestMapping(value = "/getObj")
public Object getObj(String keyredis){
Object object = redisService.get(keyredis);
if(object !=null){
IqProduct iqProduct = (IqProduct) object;
System. out.println(iqProduct.getProductName());
System. out.println(iqProduct.getId());
System. out.println(iqProduct.getTimestamp());
}return object;
}
@RequestMapping(value = "/delObj")
public boolean delObj(String keyredis)
{boolean del = redisService.del(keyredis);
return del;
}
@RequestMapping(value = "/getredis")
public String getredis(String keyredis){
String getredis = (String) redisService.getKey(keyredis);
return "redis的key是===>"+getredis;
}
@RequestMapping(value = "/delredis")
public String delredis(String keyredis){
redisService.delKey(keyredis);
return "删除成功,请通过getredis进行查询";
}
@RequestMapping(value = "/setList")
public String setList(String keyredis){
List list = new ArrayList();for (int i = 0;i<10;i++){
IqProduct iqProduct = new IqProduct();
iqProduct.setSort(1);
iqProduct.setTimestamp(new Date().getTime());
iqProduct.setProductName("productname");
list.add(iqProduct);
}
redisService.set(keyredis, list);
return "保存成功,请访问getredis查询redis";
}
@RequestMapping(value = "/getList")
public Object getList(String keyredis){
Object object = redisService.get(keyredis);
if(object !=null){
List iqProducts = (List) object;
for (int i = 0;i