非关系型数据库:redis将数据以键值对的形式存储,数据之间没有关系,存储在内存中
NOSQL系列:redis,hbase . . .
数据结构:键值对,key都是字符串,value分5种不同的数据结构
缓存的工作流程
Set(不允许重复元素)
Sortedset
通用
持久化
RDB:默认方式,不需配置
编辑redis.windows.conf文件
save 900 1 # after 900 sec (15 min) if at least 1 key changed
save 300 10 # after 300 sec (5 min) if at least 10 key changed
save 60 10000 # after 60 sec (1 min) if at least 10000 key changed
使用命令行启动服务端
redis-server.exe redis.windows.conf
AOF:日志记录方式
编辑redis.windows.conf文件
appendonly no - - > appendonly yes
appendfsync always : 每一次操作都进行持久化
appendfsync everysec : 每隔一秒进行持久化
appendfsync no : 不进行持久化
基本步骤
导入jar包
编写测试类
// 1.获取连接
Jedis jedis = new Jedis("localhost", 6379); //默认参数为("localhost", 6379)
// 2.操作
jedis.set("username", "root");
// 3.关闭连接
jedis.close();
各种数据结构的操作方法
//String
jedis.set("username", "root"); // 存储
String username = jedis.get("username"); // 获取
jedis.setex("invitecode", 60, "123456"); // 存储并在60秒后自动删除
//Hash
jedis.hset("user", "username", "root"); // 存储
jedis.hset("user", "password", "1234");
String username = jedis.hget("user", "username"); //获取单个值
Map<String, String> user = jedis.hgetAll("user"); //获取user中所有键值对
//List
jedis.lpush("mylist", "a", "b", "c"); //从左边存入
jedis.rpush("mylist", "a", "b", "c"); //从右边存入,最终结果为 c b a a b c
List<String> mylist = jedis.lrange("mylist", 0, -1); //取出指定范围的list,-1为取全部
String str1 = jedis.lpop("mylist"); //弹出最左边的元素
String str2 = jedis.rpop("mylist"); //弹出最右边的元素
//Set
jedis.sadd("myset", "java", "php", "c++"); //存储
Set<String> myset = jedis.smembers("myset"); //获取
//SortedSet
jedis.zadd("mysortedset", 5, "ailment"); //存储
Set<String> mysortedset = jedis.zrange("mysortedset", 0, -1); //取出指定范围
Jedis连接池
使用
// 0.创建配置对象,可选,有默认值
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(50);
config.setMaxIdle(10); //最大空闲连接
// 1.创建Jedis连接对象
JedisPool jedisPool = new JedisPool("localhost", 6379); //参数有默认值
// 2.获取连接
Jedis jedis = jedisPool.getResource();
// 3.使用
. . .
// 4.关闭,归还到连接池中
jedis.close();
抽取工具类
public class JedisPoolUtils{
private static JedisPool jedisPool;
static{
//读取配置文件
InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
//创建Properties对象
Properties pro = new Properties();
//关联文件
try{
pro.load(is);
} catch(IOException e){
e.printStackTrace();
}
//获取数据,设置到JedisPoolConfig中
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
config.setMaxTotal(Integer.parseInt(pro.getProperty("maxIdle")));
//初始化JedisPool
jedisPool = new JedisPool(config, pro.getProperty("host"), Integer.parseInt(pro.getProperty("port")));
}
public static Jedis getJedis(){
return jedisPool.getResource();
}
}
# jedis.properties
host=127.0.0.1
port=6379
maxTotal=50
maxIdle=10
//从缓存中查询
Jedis jedis = JedisPoolUtils.getUtils();
String province_json = jedis.get("province");
//如果缓存中没有
if(province_json == null || province_json.length() == 0){
//从关系型数据库查询
List<Province> ps = dao.findAll();
ObjectMapper mapper = new ObjectMapper();
try{
province_json = mapper.writeValueAsString(ps);
}catch(JsonProcessingException e){
e.printStackTrace();
}
//将查询结果存入缓存
jedis.set("provice", province_json);
jedis.close();
}
//返回查询结果
return province_json;
原文:https://www.cnblogs.com/whteway/p/12115134.html