目录
Redis安装——Windows+Linux环境下(2022超详细版)_叫我老伯的博客-CSDN博客
下面连接是在idea中配置maven环境,如果已经有了,这步跳过,直接在idea中新建一个maven环境就行。
配置maven以及maven环境,建立maven项目_叫我老伯的博客-CSDN博客
在pom文件中导入下面的redis依赖刷新(不要忘记,外面要加一组<dependencies>标签哦)
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.9.0</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.junit.jupiter</groupId>
- <artifactId>junit-jupiter</artifactId>
- <version>RELEASE</version>
- <scope>compile</scope>
- </dependency>
- public class TestRedis {
- @Test
- public void testRedis1(){
- Jedis jedis = new Jedis("81.70.242.198",6379, 60000);
- jedis.auth("123456789");
- jedis.set("name", "Tom");
- jedis.set("age", "18");
- System.out.println(jedis.get("name") + jedis.get("age"));
- }

/** * 验证码; * 要求 : 随机获取6位验证码 * 存入redis数据库并发送到控制台 * 手动输入到控制台并且与redis数据库内进行对比判断一致 */
- public static void main(String[] args) {
- numCode("1234567890");
- String code = new Scanner(System.in).next();
- getRedisCode("1234567890", code);
-
- }
- //获取六位验证码
- private static String getNum(){
- Random random = new Random();
- String code = "";
- for (int i = 0; i < 6; i++){
- int a = random.nextInt(10);
- code += a;
- }
- //打印到控制台
- System.out.println(code);
- return code;
- }
- //存入redis
- public static void numCode(String phone){
- Jedis jedis = new Jedis();
- //用连接池连接redis数据库
- // JedisPoolConfig config = new JedisPoolConfig();
- // JedisPool jedisPool =
- // new JedisPool(config,"81.70.242.198",6379,2000,"123456789");
- jedis = new Jedis("81.70.242.198",6379, 60000);
- jedis.auth("123456789");
- //验证码的key
- String codeKey = phone;
- //调用方法 获取验证码
- String code = getNum();
- //存入redis 设置失效时间为60s
- jedis.setex(codeKey, 60, code);
- //释放资源
- jedis.close();
- }
- //对比验证码
- public static void getRedisCode(String phone , String code){
- Jedis jedis = new Jedis();
- //用连接池连接redis数据库
- JedisPoolConfig config = new JedisPoolConfig();
- // JedisPool jedisPool =
- // new JedisPool(config,"81.70.242.198",6379,2000,"123456789");
- jedis = new Jedis("81.70.242.198",6379, 60000);
- jedis.auth("123456789");
- //验证码的key
- String codeKey = phone;
- String redisCode = jedis.get(phone);
- if (redisCode.equals(code)) {
- System.out.println("验证按证确");
- }else {
- System.out.println("验证码错误");
- }
- jedis.close();
- }

/** * 测试redis数据库的特性 * 排行榜: * 要求 x个人 每人的分数可以是随机的 * 存入到redis数据库 * 获取前三的分数信息 */
- public void testSort(){
- Jedis jedis = new Jedis();
- //用连接池连接redis数据库
- JedisPoolConfig config = new JedisPoolConfig();
- // JedisPool jedisPool =
- // new JedisPool(config,"81.70.242.198",6379,2000);
- jedis = new Jedis("81.70.242.198",6379, 60000);
-
- jedis.auth("123456789");
-
- //获取分数
- int score1 = new Random().nextInt(10);
- int score2 = new Random().nextInt(10);
- int score3 = new Random().nextInt(10);
- int score4 = new Random().nextInt(10);
- int score5 = new Random().nextInt(10);
- //存入redis set集合
- jedis.zadd("list", score1, "tom");
- jedis.zadd("list", score2, "jack");
- jedis.zadd("list", score3, "luck");
- jedis.zadd("list", score4, "zz");
- jedis.zadd("list", score5, "yy");
-
- System.out.println("获取排行榜前三 :" );
- //取出前三的数据
- Set<String> list = jedis.zrevrange("list", 0, 2);
-
- int index = 0;
- //循环遍历数据
- for(String name : list){
-
- System.out.println("第 " + ++index + ", name: " + name + "sorce :" + jedis.zscore("list" , name) );
- }
-
- }
