项目描述
上传时间
浏览人数
1.本地连接
2.服务器连接
图示:服务器获取公网IP
二,Redis调用示例:
--------------------------------------------------------
//控制层
@GetMapping("getbooks")
public JsonMsgPojo getBooks() {
return bookService.getBooks();
}
--------------------------------------------------------
//实现层
@Override
public JsonMsgPojo getBooks() {
List<BookPojo> bpLi ;
//通过关键词"getBooks" 调用redis的Value
Object o = redisService.get("getBooks");
//判断
if (o == null){ //redis中没有对应信息
//MySql查询
bpLi = bookDao.getBooks();
//把查询到的信息保存到redis中 (此时的 key为"getBooks" ,Value为bpLi)
redisService.set("getBooks",bpLi);
}else { //redis 有对应信息
//为 bpLi 赋值
bpLi = (List<BookPojo>) o ;
}
return new JsonMsgPojo<>(200,"获取图书列表成功",bpLi);
}
Redis连接/测试成功:
三.Redis常用函数
--------------------------------------------------------
//常用接口和实现层代码,可直接复制调用
//接口
import org.springframework.stereotype.Service;
@Service
public interface RedisService {
//保存数据
boolean set(final String key, Object val);
//保存数据 带失效时间
boolean set(final String key, Object val, long timeAsSecond);
//获取数据
Object get(final String key);
// 存值到redis的Map中
boolean setMap(final String key, String itemKey, Object val);
// 存值到redis的Map中 带失效时间
boolean setMap(final String key, String itemKey, Object val, long timeAsSecond);
// 取值 Map 所有
Object getMap(final String key);
// 取值 Map
Object getMap(final String key, String itemKey);
//删除key
boolean deleteByKey(final String key);
//删除Map下的指定key
boolean deleteMap(final String key, String itemKey);
//添加到List
boolean setList(final String key, Object val);
//添加到List 带失效时间
boolean setList(final String key, Object val,long timeAsSecond);
//获取List中所有数据
Object getList(final String key);
//获取list中指定索引的数据
Object getList(final String key, long idx);
}
--------------------------------------------------------
//实现层
package com.book.newbook.service.impl;
import com.book.newbook.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
@Service
public class RedisServiceImpl implements RedisService {
@Autowired
private RedisTemplate redisTemplate;
//保存数据
@Override
public boolean set(String key, Object val) {
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, val);
return true;
} catch (Exception e) {
return false;
}
}
//保存数据 带失效时间
public boolean set(final String key, Object val, long timeAsSecond) {
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, val);
redisTemplate.expire(key, timeAsSecond, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
return false;
}
}
//获取数据
@Override
public Object get(String key) {
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
return operations.get(key);
} catch (Exception e) {
return null;
}
}
// 存值到redis的Map中
@Override
public boolean setMap(String key, String itemKey, Object val) {
try {
redisTemplate.opsForHash().put(key, itemKey, val);
return true;
} catch (Exception e) {
return false;
}
}
// 存值到redis的Map中 带失效时间
@Override
public boolean setMap(String key, String itemKey, Object val, long timeAsSecond) {
try {
redisTemplate.opsForHash().put(key, itemKey, val);
redisTemplate.expire(key, timeAsSecond, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
return false;
}
}
// 取值 Map 所有
@Override
public Object getMap(String key) {
try {
return redisTemplate.opsForHash().entries(key);
} catch (Exception e) {
return null;
}
}
// 取值 Map
@Override
public Object getMap(String key, String itemKey) {
try {
return redisTemplate.opsForHash().get(key, itemKey);
} catch (Exception e) {
return null;
}
}
//删除key
@Override
public boolean deleteByKey(String key) {
return redisTemplate.delete(key);
}
//删除Map下的指定key
@Override
public boolean deleteMap(String key, String itemKey) {
long deleted = redisTemplate.opsForHash().delete(key, itemKey);
return deleted != 0 ? true : false;
}
//添加到List
@Override
public boolean setList(String key, Object val) {
long added = redisTemplate.opsForList().rightPush(key, val);
return added != 0 ? true : false;
}
//添加到List 带失效时间
@Override
public boolean setList(String key, Object val, long timeAsSecond) {
long added = redisTemplate.opsForList().rightPush(key, val);
redisTemplate.expire(key, timeAsSecond, TimeUnit.SECONDS);
return added != 0 ? true : false;
}
//获取List中所有数据
@Override
public Object getList(String key) {
return redisTemplate.opsForList().range(key, 0, -1);
}
@Override
public Object getList(String key, long idx) {
return redisTemplate.opsForList().range(key, 0, -1).get((int) idx);
}
}
--------------------------------------------------------