暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Linux环境搭建与Redis应用

程序世界之路 2022-02-14
239

redis下载地址 https://redis.io/


Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.


From source code

Download, extract and compile Redis with

$ 在虚拟机进入usr目录,创建mkdir redis
$ wget https://download.redis.io/releases/redis-6.2.6.tar.gz
$ tar xzf redis-6.2.6.tar.gz
$ cd redis-6.2.6
$ make

The binaries that are now compiled are available in the src
 directory. Run Redis with

$ 启动Redis数据库
cd / redis
yum install gcc
yum install wget


开发端口
firewall-cmd --zone=public --add-port=6379/tcp --permanent


设置防火墙
firewall-cmd --reload


The binaries that are now compiled are available in the src directory. Run Redis with:


You can interact with Redis using the built-in client

$ 使用客户端编辑数据库
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"


start-up start and stop

$ 启动redis服务
./src/redis-server redis.conf
$ 查看是否启动redis
netstat -tulpn


$ 关闭进程
./src/redis-cli shutdown

Redis的常见配置

配置项
示例
说明
daemonize
‍daemonize yes
是否启用后台运行,默认no
portport 6379
设置端口号,默认6379
logfile
logfile 日志文件
设置日志文件
databases
databases 255设置redis数据库总量
dir
dir 数据文件目录
设置数据文件存储目录
requirepass
requirepass admin设置使用密码

Hash命令


命令
示例
说明
hgethget student age
获取hash中key=age
hset
hset student age 23
设置hash中age=23

hmset

hmget

hgetall

hmset student age 30 name 18

hgetall student

设置hash多个值

获取hash多个值

获取hash所有值

hdel
hdel student age
删除student的age
hexists
hexists student name检查是否存在
hlen
hlen student获取指定长度



code&&java

Jedis is a Java client for Redis designed for performance and ease of use.

https://github.com/redis/jedis


Getting started

To get started with Jedis, first add it as a dependency in your Java project. If you're using Maven, that looks like this:

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
      <version>2.9.0</version>
</dependency>


<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>

Next, you'll need to connect to Redis. For many applications, it's best to use a connection pool. You can instantiate a Jedis connection pool like so:

JedisPool pool = new JedisPool("服务器地址"6379);

Once you have a JedisPool
 instance, you can use a try-with-resources block to get a connection and run Redis commands.


try (Jedis jedis = pool.getResource()) {
jedis.set("clientName", "Jedis");
}


JedisTestor.java

package com.imooc.jedis;


import java.util.List;


import redis.clients.jedis.Jedis;


public class JedisTestor {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.182.144",6379);
try {
jedis.select(2);
System.out.println("Redis连接成功");

-------- 添加字符串 --------
// 设置sn数据
jedis.set("sn","7781-9938");
// 获取数据
String sn = jedis.get("sn");
System.out.println(sn);
// 设置多条数据
jedis.mset(new String[] {"title","java书籍","num","20"});
// 接收List集合
List<String> goods = jedis.mget(new String[] {"sn","title","num"});
System.out.println(goods);
// 数字类型自增+1
Long num = jedis.incr("num");
System.out.println(num);



      --------      添加Hash        -------- 
jedis.hset("student:3312", "name", "小明");
String name = jedis.hget("student:3312", "name");
System.out.println(name);
Map<String,String> map = new HashMap<String,String>();
map.put("name", "我是king");
map.put("age","18");
map.put("id","3313");
      jedis.hmset("student:3113", map);
Map<String,String> smap = jedis.hgetAll("student:3113");
System.out.println(smap);


-------- 添加集合 --------
jedis.del("letter");
jedis.rpush("letter", new String[] {"d","e","f"});
jedis.lpush("letter", new String[] {"c","b","a"});
jedis.lpop("letter");
jedis.rpop("letter");
List<String> list = jedis.lrange("letter", 0, -1);
System.out.print(list);

} catch(Exception e) {
e.printStackTrace();
jedis.close();
}

}
}




e

文章转载自程序世界之路,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论