redis学习
来源:互联网 发布:ipadapp下载不了软件 编辑:程序博客网 时间:2024/06/15 00:43
我们常用的数据库一般是关系型数据库,具有严格的二维视图,而非关系行数据库是关系行数据库的补充,它的语法相对简单,最重要的是,它的存储效率非常的高,非常适合作为缓存使用。
常见的非关系型数据库比较有名的是memcached 和redis ,一般我们使用的比较多的是redis,因为其功能较memcached更为强大.现在就介绍下redis数据库的使用
1.redis数据的安装
redis数据库的安装非常简单,直接从官方下载对应版本的(32 64位)的安装包,解压到文件夹中.解压后会有5个exe文件和一个 conf文件
Redis-server –> redis服务器
Redis-cli –> redis的客户端
Redis-benchmark –>redis性能测试工具
Redis-check-aof –>aof文件修复工具
Redis-check-dump –> RDB文件检查工具
redis.windows.conf –>redis的配置文件
2.安装多个redis数据库
非常的简单,只需要复制一下redis的安装包,修改conf文件的端口,这样就能够同时使用多个redis数据库
3.redis数据库的使用
打开方式有两种:
3.1、直接双击Redis-server.exe
3.2、在安装目录打开cmd窗口,redis-cli -h 127.0.0.1 -p 6379
4.redis数据库服务的注册,启动和停止命令
4.1注册服务:redis-server–service-install redis.windows.conf–loglevelverbose
4.2卸载服务:redis-server–service-uninstall
4.3启动redsi:redis-server–service-start
4.4停止redis:redis-server–service-stop
5.redis客户端的使用
redis的java客户端有很多个,官方推荐的有jedis以及Redisson
这篇文章就介绍jedis的操作,下次再介绍Redisson的使用
jedis的依赖
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.2</version> </dependency>
5.1 jedis的简单例子
import redis.clients.jedis.Jedis;public class JedisDemo { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1",6379); jedis.set("jedis-test", "123"); String value = jedis.get("jedis-test"); System.out.println(value);//123 jedis.close(); }}
5.2 jedis使用连接池
import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;/** * jedis连接池的使用 */public class JedisPoolDemo { public static void main(String[] args) { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); //设置最大的连接数和空闲数 jedisPoolConfig.setMaxTotal(100); jedisPoolConfig.setMaxIdle(2); //构建连接池 JedisPool jedisPool = new JedisPool(jedisPoolConfig,"127.0.0.1",6379); Jedis jedis = jedisPool.getResource(); System.out.println(jedis.get("jedis-test"));//123 //将连接还给连接池并释放连接池 jedisPool.returnBrokenResource(jedis); jedisPool.close(); }}
5.3 jedis使用分片式集群
import java.util.ArrayList;import java.util.List;import redis.clients.jedis.JedisPoolConfig;import redis.clients.jedis.JedisShardInfo;import redis.clients.jedis.ShardedJedis;import redis.clients.jedis.ShardedJedisPool;/** * 分片式jedis集群的使用 */public class SharedJedisPoolDemo { public static void main(String[] args) { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(100); jedisPoolConfig.setMaxIdle(2);//最大空闲数 //定义集群信息 List<JedisShardInfo> shads = new ArrayList<JedisShardInfo>(); shads.add(new JedisShardInfo("127.0.0.1",6379)); shads.add(new JedisShardInfo("192.168.232.1",6380)); //定义集群连接信息 ShardedJedisPool shardedJedisPool = new ShardedJedisPool(jedisPoolConfig,shads); ShardedJedis jedis = null; try { //可以从两个redis数据库中获取值 jedis = shardedJedisPool.getResource(); System.out.println(jedis.get("Emp_4")); System.out.println(jedis.get("jedis-test")); } catch (Exception e) { e.printStackTrace(); }finally { if(null != jedis){ jedis.close(); } } shardedJedisPool.close(); }}
6.spring集成redis的使用
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- redis的配置文件 poolConfig ShardedJedisPool --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"< <property name="maxTotal" value="{redis.maxTotal}"></property> </bean> <bean class="redis.clients.jedis.ShardedJedisPool"> <constructor-arg index = "0" value="poolConfig"/> <constructor-arg index = "1" > <list> <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0" value="${redis.model1.host}"/> <constructor-arg index="1" value="${redis.nodel1.port}"/> </bean> </list> </constructor-arg> </bean></beans>
7.redis的主要方法
@Servicepublic class RedisService { @Autowired(required= false) private ShardedJedisPool shardedJedisPool; public <T> T execute(Fun<T,ShardedJedis> fun){ ShardedJedis shardedJedis = null; try { //从连接池中获取资源,作为Fun接口的参数 shardedJedis = shardedJedisPool.getResource(); //返回一个接口的方法,具体返回值不确定,需要子类去指定 return fun.callback(shardedJedis); } catch (Exception e) { e.printStackTrace(); }finally { if(null != shardedJedis){ shardedJedis.close(); } } return null; } /** * set方法,指定接口Fun的方法execute的返回值为String,使用了匿名内部类, * 参数参数必须为final修饰 */ public String set(final String key,final String value){ return this.execute(new Fun<String,ShardedJedis>(){ @Override public String callback(ShardedJedis e) { return e.set(key, value); } }); } /** * 设置key的时候,同时设置有效时间 */ public String set(final String key,final String value,final Integer seconds){ return this.execute(new Fun<String,ShardedJedis>(){ @Override public String callback(ShardedJedis e) { return e.setex(key, seconds, value); } }); } /** * get 方法 */ public String get(final String key){ return this.execute(new Fun<String,ShardedJedis>(){ @Override public String callback(ShardedJedis e) { return e.get(key); } }); } /** * del方法 */ public Long del(final String key){ return this.execute(new Fun<Long,ShardedJedis>(){ @Override public Long callback(ShardedJedis e) { return e.del(key); } }); } /** * 设置key的有效时间 */ public Long expire(final String key,final Integer seconds){ return this.execute(new Fun<Long,ShardedJedis>(){ @Override public Long callback(ShardedJedis e) { return e.expire(key, seconds); } }); }}
- redis学习:redis安装
- 【Redis学习】:Redis数据类型
- redis 学习
- 学习redis
- 学习Redis
- redis学习
- redis学习
- Redis学习
- redis 学习
- Redis学习
- redis学习
- redis 学习
- redis学习
- redis学习
- Redis学习
- 学习redis
- Redis学习
- Redis学习
- c#继承中的函数调用实例
- koa-router源码分析
- hdu4027(线段树)
- 三种LCA算法(一):Doubly算法(倍增算法)
- @media 媒体查询
- redis学习
- hdu6170-多看几遍之DP&递推&字符串-Two strings
- c# 如何调用非托管函数
- ImportError: No module named 'pip._vendor.retrying'
- JVM参数设置、分析
- jzoj5331 【NOIP2017提高A组模拟8.23】壕游戏
- 测试图片
- Array Nesting 问题及解法
- Sublime Text 3 配置文件详解