windows - redis内存对象使用

一、下载Redis

  1. 下载Redis并解压缩https://redis.io/download
  2. 在解压文件夹开启命令行。

G:\Software\redis-64.3.0.503>redis-server.exe redis.windows.conf
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.0.503 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 3836
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

[3836] 29 Nov 16:39:12.287 # Server started, Redis version 3.0.503
[3836] 29 Nov 16:39:12.301 * DB loaded from disk: 0.006 seconds
[3836] 29 Nov 16:39:12.303 * The server is now ready to accept connections on port 6379

二、测试简单读取写入

参数:
-h 目标主机IP地址
-p 目标主机Redis服务器监听端口

G:\Software\redis-64.3.0.503>redis-cli.exe -h 127.0.0.1 -p 6379
127.0.0.1:6379> set me wyf
OK
127.0.0.1:6379> get me
"wyf"




三、maven web程序中使用redis

pom.xml添加依赖包

    <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.4.2</version>
    </dependency>


  • 简单的使用,创建Jedis对象传入主机IP地址,端口。并通过get方法获取对象。结束后调用close方法关闭
  • 不同的客户端都可以进行访问,因此可以实现内存对象共享功能
    @RequestMapping("/g-greet")
    public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {

        String host = "127.0.0.1";
        int port = 6379;
        Jedis jedis = new Jedis(host,port);
        String strInRedis = jedis.get(name);
        jedis.close();
        
        model.addAttribute("name", strInRedis);
        return "greeting";
    }




















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值