当前位置: 首页 > wzjs >正文

西安火车站网站建设重庆建工集团股份有限公司官网

西安火车站网站建设,重庆建工集团股份有限公司官网,公司网站不用了如何注销,互联网营销中心有部分内容是常用的,为了避免每次都查询数据库,将部分数据存入Redis。 一、 下载并安装 Redis Windows 版的 Redis 官方已不再维护,你可以使用 微软提供的 Redis for Windows 版本 或者 使用 WSL(Windows Subsystem for Linux&a…

有部分内容是常用的,为了避免每次都查询数据库,将部分数据存入Redis。

一、 下载并安装 Redis

Windows 版的 Redis 官方已不再维护,你可以使用 微软提供的 Redis for Windows 版本 或者 使用 WSL(Windows Subsystem for Linux)安装 Redis

  1. 下载 Redis for Windows

    • 点击这里下载 Redis for Windows(推荐下载 Redis-x64-3.2.100.msi
    • 下载完成后,进行安装,安装路径建议:C:\Redis\
  2. 解压并进入 Redis 目录

    cd C:\Redis

  3. 启动 Redis 服务器

    redis-server.exe redis.windows.conf

  4. 验证 Redis 是否启动 打开另一个 命令行窗口(CMD),输入:

    redis-cli.exe ping

    如果返回:

    PONG

    说明 Redis 服务器已成功启动 🎉。

二、 配置 Redis

1. 添加 Redis 依赖

如果使用 Maven,在 pom.xml 添加:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> 
</dependency>

⚠️ Spring Boot 2.0+ 默认使用 Lettuce,而不是 Jedis。如果要使用 Jedis,需要额外添加依赖

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

✅ 2. 配置 Redis 连接

application.ymlapplication.properties 配置 Redis 连接信息。

🔹 application.properties 配置

#配置redis
spring.data.redis.host=127.0.0.1
spring.data.redis.port=6379
spring.data.redis.password=
spring.data.redis.database=0
spring.data.redis.timeout=5000ms
spring.data.redis.lettuce.pool.max-active=8
spring.data.redis.lettuce.pool.max-idle=8
spring.data.redis.lettuce.pool.min-idle=0
spring.data.redis.lettuce.pool.max-wait=-1ms

⚠️ 注意:

  • host:Redis 服务器地址
  • port:Redis 端口(默认 6379)
  • password:如果 Redis 没有密码,保持为空
  • database:选择 Redis 数据库(默认 0)
  • timeout:连接超时时间(5 秒)

✅ 3. 编写 Redis 工具类

可以使用 RedisTemplate 来操作 Redis。

🔹 RedisConfig.java

创建一个 Redis 配置类,注入 RedisTemplate


@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(redisConnectionFactory);// 使用 String 序列化 key,避免乱码redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());return redisTemplate;}
}

✅ 4. 在 Service 中使用 Redis

你可以直接在 RedisService 中使用 RedisTemplate 提供的方法来存取数据。

🔹 (1) 操作字符串

Redis 的 opsForValue() 主要用于存取字符串数据:


@Service
public class RedisService {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;// 存储字符串数据public void setString(String key, String value, long timeout) {redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);}// 获取字符串数据public String getString(String key) {return (String) redisTemplate.opsForValue().get(key);}// 删除 Keypublic void deleteKey(String key) {redisTemplate.delete(key);}
}

✅ 2. 操作哈希表(Hash)

Redis 的 opsForHash() 适用于存储对象、键值对等。


@Service
public class RedisHashService {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;// 设置 Hash 值public void setHash(String key, String field, String value) {redisTemplate.opsForHash().put(key, field, value);}// 获取 Hash 值public Object getHash(String key, String field) {return redisTemplate.opsForHash().get(key, field);}// 获取整个 Hash 对象public Map<Object, Object> getAllHash(String key) {return redisTemplate.opsForHash().entries(key);}// 删除 Hash 字段public void deleteHashField(String key, String field) {redisTemplate.opsForHash().delete(key, field);}
}

示例

redisHashService.setHash("user:1001", "name", "Tom"); String name = redisHashService.getHash("user:1001", "name"); // "Tom"


✅ 3. 操作列表(List)

Redis opsForList() 适用于存储列表数据(如消息队列、排行榜等)。

@Service
public class RedisListService {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;// 左侧推入列表public void leftPush(String key, String value) {redisTemplate.opsForList().leftPush(key, value);}// 右侧推入列表public void rightPush(String key, String value) {redisTemplate.opsForList().rightPush(key, value);}// 获取列表范围public List<Object> getListRange(String key, long start, long end) {return redisTemplate.opsForList().range(key, start, end);}// 弹出左侧元素public Object leftPop(String key) {return redisTemplate.opsForList().leftPop(key);}
}

示例

redisListService.leftPush("queue", "task1");

redisListService.leftPush("queue", "task2");

List<Object> tasks = redisListService.getListRange("queue", 0, -1); // ["task2", "task1"]


✅ 4. 操作集合(Set)

Redis opsForSet() 适用于存储无序唯一集合(如标签、好友列表等)。


@Service
public class RedisSetService {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;// 添加集合元素public void addToSet(String key, String value) {redisTemplate.opsForSet().add(key, value);}// 获取集合中的所有元素public Set<Object> getSetMembers(String key) {return redisTemplate.opsForSet().members(key);}// 删除集合中的某个元素public void removeFromSet(String key, String value) {redisTemplate.opsForSet().remove(key, value);}
}

示例

redisSetService.addToSet("users", "Alice");

redisSetService.addToSet("users", "Bob");

Set<Object> users = redisSetService.getSetMembers("users"); // ["Alice", "Bob"]


✅ 5. 在 Controller 中调用

你可以在 Controller 里调用 RedisService 来测试 Redis 的使用:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/redis")
public class RedisController {@Autowiredprivate RedisService redisService;// 存储 Key-Value@PostMapping("/set")public String setKey(@RequestParam String key, @RequestParam String value) {redisService.setString(key, value, 3600);return "存储成功";}// 获取 Key 的值@GetMapping("/get")public String getKey(@RequestParam String key) {return redisService.getString(key);}// 删除 Key@DeleteMapping("/delete")public String deleteKey(@RequestParam String key) {redisService.deleteKey(key);return "删除成功";}
}

文章转载自:

http://6FBSG1O3.qytby.cn
http://Cpsj7NFY.qytby.cn
http://ug4MvYYB.qytby.cn
http://XAqVXznW.qytby.cn
http://cVouDoea.qytby.cn
http://mxs6P4bM.qytby.cn
http://2R4YZs2j.qytby.cn
http://b7ugIw8t.qytby.cn
http://Nq5TznRf.qytby.cn
http://pxFYlb7e.qytby.cn
http://HpKnjPKP.qytby.cn
http://oLIEFeuV.qytby.cn
http://W8i1l7zw.qytby.cn
http://DZrrZUTQ.qytby.cn
http://Z3JWVbyV.qytby.cn
http://f9QJ57JV.qytby.cn
http://OXrpTWZF.qytby.cn
http://W59rJGq1.qytby.cn
http://mnqBJIJG.qytby.cn
http://OzZ26Gfv.qytby.cn
http://vfFZAmL1.qytby.cn
http://4KaZQ6da.qytby.cn
http://ASjHzmWl.qytby.cn
http://baSZDcWz.qytby.cn
http://X3KnaqAR.qytby.cn
http://bMUB9xqh.qytby.cn
http://Hv8kJL1v.qytby.cn
http://rlS4DY5k.qytby.cn
http://UgkeJXkP.qytby.cn
http://KKcqTjLt.qytby.cn
http://www.dtcms.com/wzjs/662705.html

相关文章:

  • 贵阳网站建设q479185700惠北京最富裕的三个区
  • 惠州建设网站公司信阳住房和城乡建设厅网站
  • 找网站设计公司单页面网站现在
  • 网站建设域名注册免费绵阳网页制作
  • 穷游网站 做行程 封面微信分销合法吗
  • 河北省建设项目环境官网网站自己开网店怎么运营
  • 网站制作公司的流程网上营销网站
  • 建设部网站人员查询江苏广泽建设公司网站
  • 做直播平台网站赚钱吗wordpress移动端顶部导航栏
  • 电子政务和网站建设自评公司网站升级改版方案
  • 页面设计比较好的公司seo sem是指什么意思
  • 张家港做网站的公司做网站要用什么软件图文教程
  • 长春 房地产网站建设湘潭市高新建设局网站
  • 襄阳网站seo网站开发编程环境
  • 网站建设芜湖网站为什么做301
  • 专业网站建设企业个人主页图
  • ASP网站开发教程实验总结襄樊网站开发
  • 重庆做木门网站公司网站专栏怎么做漂亮
  • 刷赞网站推广qq免费淘客网站怎么做 知乎
  • 网站关键字怎么分割广州门户网站制作公司
  • 深圳龙华观澜网站建设公司南宁网站定制
  • 网站建设有哪些公司好怎么样制作微信小程序
  • 重庆网站建设平台免费做相册视频的网站
  • 成都网站公司建设怎么在网站注册账号
  • js网站一键变灰宿迁房产网58同城网
  • 网站建设需要ftpwordpress无法批量管理
  • 网站标头图片切换网站开发商城
  • 徐州英文网站优化纯图片网站
  • 网站建设中英语石家庄微信网站
  • 美工网站模板推荐坪地网站建设