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

做网站的最终目的百度网盘免费下载

做网站的最终目的,百度网盘免费下载,漳州最专业的网站建设公司,个人可以做b2b网站1. 准备工作 环境说明 java 8;redis7.2.2,redis集成RedisSearch、redisJson 模块;spring boot 2.5在执行 redis 命令, 或者监控 程序执行的redis 指令时,可以采用 redisinsight查看,下载地址。 背景说明 需…

1. 准备工作

  1. 环境说明

    • java 8;redis7.2.2,redis集成RedisSearch、redisJson 模块;spring boot 2.5
    • 在执行 redis 命令, 或者监控 程序执行的redis 指令时,可以采用 redisinsight查看,下载地址。
  2. 背景说明

    • 需要对在线的用户进行搜索,之前是存储成 string, 每次搜索需要先全部遍历,然后加载到内存,然后进行筛选。十分消耗性能并且速度极慢。使用 redisJson + redisSearch 可以极大的优化查询性能
    • 项目后期需要支持全文搜索。
  3. 实现思路:采用 RedisTemplate, 执行 lua 脚本。

2. 实现

2.1 配置

引入依赖

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

配置 redisTermplate, 配置 lua 脚本,便于 redisTemplate 执行[^2]

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport
{@Bean@SuppressWarnings(value = { "unchecked", "rawtypes" })public RedisTemplate<String, Object> redisTemplate1(RedisConnectionFactory connectionFactory){RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(connectionFactory);RedisSearchSerializer serializer = new RedisSearchSerializer(Object.class);// 使用StringRedisSerializer来序列化和反序列化redis的key值template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(serializer);// Hash的key也采用StringRedisSerializer的序列化方式template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(serializer);template.afterPropertiesSet();return template;}// lua 脚本配置@Beanpublic DefaultRedisScript<String> jsonSetScript(){DefaultRedisScript<String> redisScript = new DefaultRedisScript<>();redisScript.setScriptText("return redis.call('JSON.SET', KEYS[1], '$', ARGV[1]);");redisScript.setResultType(String.class);return redisScript;}@Beanpublic DefaultRedisScript<Object> jsonGetScript(){DefaultRedisScript<Object> redisScript = new DefaultRedisScript<>();redisScript.setScriptText("return redis.call('JSON.GET', KEYS[1]);");redisScript.setResultType(Object.class);return redisScript;}@Beanpublic DefaultRedisScript<List> jsonSearchScript(){DefaultRedisScript<List> redisScript = new DefaultRedisScript<>();redisScript.setScriptText("local offset = tonumber(ARGV[2])\n" +"local count = tonumber(ARGV[3])\n" +"return redis.call('FT.SEARCH', KEYS[1], ARGV[1], 'return', 0, 'limit', offset, count);");redisScript.setResultType(List.class);return redisScript;}
}

RedisSearchSerializer 序列化配置


import com.alibaba.fastjson2.JSON;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;import java.nio.charset.Charset;/*** Redis使用FastJson序列化** @author ruoyi*/
public class RedisSearchSerializer<T> implements RedisSerializer<T> {public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");private Class<T> clazz;public RedisSearchSerializer(Class<T> clazz) {super();this.clazz = clazz;}@Overridepublic byte[] serialize(T t) throws SerializationException {if (t == null) {return new byte[0];}if (t instanceof String) {return ((String)t).getBytes(DEFAULT_CHARSET);}return JSON.toJSONString(t).getBytes(DEFAULT_CHARSET);}@Overridepublic T deserialize(byte[] bytes) throws SerializationException {if (bytes == null || bytes.length <= 0) {return null;}String str = new String(bytes, DEFAULT_CHARSET);// 不是 json 也不是 序列化的字符串,那就只能是数字,如果不是数字直接返回if (!str.startsWith("{") && !str.startsWith("[") && !str.startsWith("\"") && !str.matches("^\\d*$")) {return clazz.cast(str);}return JSON.parseObject(str, clazz);}
}

数据实体类

@Data
public class LoginUser {private String ipaddr;private String username;public LoginUser(String ipaddr, String username) {this.ipaddr = ipaddr;this.username = username;}
}

2.2 封装 对 json的 操作

redisService


@Service
public class RedisService {@Autowiredprivate RedisScript<String> jsonSetScript;@Autowiredprivate RedisScript<Object> jsonGetScript;@Autowiredprivate RedisScript<List> jsonSearchScript;@Autowiredprivate RedisTemplate<String, Object> redisTemplate1;public LoginUser getLoginUser(String uuid) {String key = RedisKeys.LOGIN_TOKEN_KEY + uuid;JSONObject obj = (JSONObject) redisTemplate1.execute(this.jsonGetScript, Collections.singletonList(key));if (obj == null) {return null;}return obj.toJavaObject(LoginUser.class);}public void setLoginUser(String uuid, LoginUser loginUser, int expireTime, TimeUnit unit) {String key = RedisKeys.LOGIN_TOKEN_KEY + uuid;redisTemplate1.execute(this.jsonSetScript, Collections.singletonList(key), loginUser);redisCache.expire(key, expireTime, unit);}public Page<String> searchLoginUser(String query, Pageable page) {List list = redisTemplate1.execute(this.jsonSearchScript,Collections.singletonList("login_tokens_idx"),query, page.getOffset(), page.getPageSize());Long total = (Long) list.get(0);List<String> ids = new ArrayList<>();for (int i = 1; i < list.size(); i++) {ids.add(((String) list.get(i)).replaceAll(RedisKeys.LOGIN_TOKEN_KEY, ""));}return new PageImpl<>(ids, page, total);}public interface RedisKeys {String LOGIN_TOKEN_KEY = "login_tokens1:";}
}

2.3 在 redis 中创建索引

redis 创建索引[^1], 其中 ipaddr 是 IP 字段,包含 “.” 等特殊字符,所以需要将 索引中的 ipaddr 设置成 tag 类型,才能搜索到[^3]

# 连接redis, 如果使用 redisinsight 则不需要这步
redis-cli -a "password"
# 创建索引
FT.CREATE login_tokens_idx on JSON prefix 1 "login_tokens1:"SCHEMA $.ipaddr tag $.username text

3. 测试


import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;import java.util.concurrent.TimeUnit;@RunWith(SpringRunner.class)
@SpringBootTest(classes = YourApplication.class)
@ActiveProfiles("prod-local")
public class RedisServiceTest {@Autowiredprivate RedisService redisService;@Testpublic void testSetAndGetLoginUser() {LoginUser user = new LoginUser("192.168.1.1", "testUser");redisService.setLoginUser("123456", user, 60, TimeUnit.SECONDS);LoginUser getUser = redisService.getLoginUser("123456");Assert.assertEquals(user.getIpaddr(), getUser.getIpaddr());Assert.assertEquals(user.getUsername(), getUser.getUsername());}@Testpublic void testDeleteLoginUser() {LoginUser user = new LoginUser("192.168.1.1", "testUser");redisService.setLoginUser("123456", user, 60, TimeUnit.SECONDS);redisService.deleteLoginUser("123456");LoginUser getUser = redisService.getLoginUser("123456");Assert.assertNull(getUser);}@Testpublic void testSearchLoginUser() {// 添加测试数据LoginUser user1 = new LoginUser("192.168.1.1", "user1");LoginUser user2 = new LoginUser("192.168.1.2", "user2");redisService.setLoginUser("123456", user1, 60, TimeUnit.SECONDS);redisService.setLoginUser("789012", user2, 60, TimeUnit.SECONDS);// 搜索测试Page<String> page = redisService.searchLoginUser("user*", PageRequest.of(0, 10));Assert.assertEquals(page.getTotalElements(), 2);Assert.assertEquals(page.getContent().size(), 2);Assert.assertTrue(page.getContent().contains("123456"));Assert.assertTrue(page.getContent().contains("789012"));}
}
http://www.tj-hxxt.cn/news/89098.html

相关文章:

  • 单页购物网站源码舆情优化公司
  • 服务器 打开网站iis7google官网入口下载
  • 自己怎么健网站视频教程开网店如何运营和推广
  • 设计模版网站万网官网登录
  • 网站建设属于移动互联网广州发布紧急通知
  • 为什么网站要域名百度电脑版下载安装
  • 娄底哪里做网站网络营销模式有哪些
  • 鞍山网站制作公司厦门小鱼网
  • wordpress如何设置标题字的大小seo外链推广员
  • aspnet动态网站开发题目推广平台排名
  • 做b2b比较好的网站有哪些seo怎么刷排名
  • 免费建企业网站网站建设推广专家服务
  • 设计比较有特色的网站最新足球消息
  • 简单的电影网站模板在线seo优化
  • 成品网站价格表百度软件商店
  • 做建材网站设计培训班学费一般多少
  • 建设美食电子商务网站南宁seo结算
  • 用自己的服务器建网站国内免费b2b网站大全
  • 做商品二维码检测的网站哈尔滨网络优化推广公司
  • 网站建设公司违法如何免费做网站
  • 做游戏能赚钱的网站怎么样把自己的产品网上推广
  • 做网站如何屏蔽中国的ip企业做推广有几种方式
  • 江苏 建设 招标有限公司网站优化大师优化项目有
  • 网站改了模板被百度降权微信营销的方法和技巧
  • wordpress浏览器主题360优化大师
  • 企业网站建设的原则包括搜索引擎优化的目的是对用户友好
  • 世界优秀摄影作品网站网络营销中的四种方法
  • 旅游网站只做推广引流方法有哪些推广方法
  • 注册网站的好处岳阳seo快速排名
  • 在上海做兼职去哪个网站搜索百度网盘官方