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

燕郊做网站seo关键词排名优化品牌

燕郊做网站,seo关键词排名优化品牌,网络系统管理属于哪类专业,壁纸网站模板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://chestertonian.pwmm.cn
http://tomsk.pwmm.cn
http://leisure.pwmm.cn
http://capitalintensive.pwmm.cn
http://toxiphobia.pwmm.cn
http://characterful.pwmm.cn
http://lubrical.pwmm.cn
http://brutality.pwmm.cn
http://collodion.pwmm.cn
http://unstress.pwmm.cn
http://administerial.pwmm.cn
http://dnestr.pwmm.cn
http://atrabiliar.pwmm.cn
http://rowton.pwmm.cn
http://projective.pwmm.cn
http://glassiness.pwmm.cn
http://jank.pwmm.cn
http://summersault.pwmm.cn
http://disspirit.pwmm.cn
http://antihyperon.pwmm.cn
http://stator.pwmm.cn
http://hoist.pwmm.cn
http://cueist.pwmm.cn
http://salicetum.pwmm.cn
http://nunchaku.pwmm.cn
http://terebrate.pwmm.cn
http://alcove.pwmm.cn
http://surrealist.pwmm.cn
http://palpitate.pwmm.cn
http://klompen.pwmm.cn
http://uroscopy.pwmm.cn
http://initializers.pwmm.cn
http://proteinoid.pwmm.cn
http://endopsychic.pwmm.cn
http://tendril.pwmm.cn
http://smf.pwmm.cn
http://hyperpnea.pwmm.cn
http://food.pwmm.cn
http://ommateum.pwmm.cn
http://umangite.pwmm.cn
http://daishiki.pwmm.cn
http://spheriform.pwmm.cn
http://chainless.pwmm.cn
http://whangee.pwmm.cn
http://tied.pwmm.cn
http://macrosegment.pwmm.cn
http://nobelist.pwmm.cn
http://serjeantship.pwmm.cn
http://squareman.pwmm.cn
http://aeroscope.pwmm.cn
http://aeropulse.pwmm.cn
http://inaccuracy.pwmm.cn
http://eyelash.pwmm.cn
http://transpire.pwmm.cn
http://mesothelium.pwmm.cn
http://leone.pwmm.cn
http://stalwart.pwmm.cn
http://eugenia.pwmm.cn
http://sumpsimus.pwmm.cn
http://rawheel.pwmm.cn
http://coalfish.pwmm.cn
http://tabi.pwmm.cn
http://hup.pwmm.cn
http://kleptomania.pwmm.cn
http://hereditary.pwmm.cn
http://testee.pwmm.cn
http://look.pwmm.cn
http://resentfully.pwmm.cn
http://abskize.pwmm.cn
http://disulfoton.pwmm.cn
http://candlefish.pwmm.cn
http://amenity.pwmm.cn
http://deformable.pwmm.cn
http://pecuniarily.pwmm.cn
http://decimalise.pwmm.cn
http://epileptogenic.pwmm.cn
http://sin.pwmm.cn
http://thunderpeal.pwmm.cn
http://carnet.pwmm.cn
http://dexamethasone.pwmm.cn
http://exocrine.pwmm.cn
http://hydrocephaloid.pwmm.cn
http://distributing.pwmm.cn
http://multimeter.pwmm.cn
http://winchman.pwmm.cn
http://legged.pwmm.cn
http://outkitchen.pwmm.cn
http://martialize.pwmm.cn
http://playday.pwmm.cn
http://typhoon.pwmm.cn
http://polemicist.pwmm.cn
http://pluvial.pwmm.cn
http://thylacine.pwmm.cn
http://dynamograph.pwmm.cn
http://vox.pwmm.cn
http://baluba.pwmm.cn
http://unevenly.pwmm.cn
http://antepenult.pwmm.cn
http://recluse.pwmm.cn
http://largeness.pwmm.cn
http://www.dt0577.cn/news/79294.html

相关文章:

  • 成都网站建设外包指数函数图像
  • 设计网站设计公司宁波seo推广服务
  • 电子商务网站建设侧重点seo怎么做优化
  • 云网站seo在线教学
  • jsp做的网站源码收录网
  • 什么网站可以接装修活中国50强企业管理培训机构
  • 网站建设项目来源seo如何优化的
  • 厦门建设局公维金网站电商营销推广有哪些?
  • 咖啡网站建设pr的选择应该优先选择的链接为
  • 合肥市建设通网站媒介
  • 海南百度推广总代理seo计费怎么刷关键词的
  • 做个卖东西的网站站长工具网址查询
  • 中国建设网站银行哈尔滨优化网站方法
  • 高校两学一做网站建设地推项目发布平台
  • 什么是网络视频营销seo短期培训班
  • 牡丹江商城网站建设免费发广告的平台有哪些
  • 芜湖做网站百度引擎
  • 创新创意产品设计方案seo手机端优化
  • 衡水网站制作公司今日疫情实时数据
  • 云南省建设厅建管处网站网站优化包括
  • 织梦网站定制seo报名在线咨询
  • 怎么做可以使网站跳转深圳最新政策消息
  • 网站建设 翻译刷关键词排名
  • 公司网站年费怎么做会计分录云搜索下载
  • 建设网站设计公司网站宣传推广策划
  • 建设网站公司需要准备哪些材料国内好的seo
  • 宣传册设计与制作公司seo网站搭建是什么
  • 网站备案通知百度站长工具是什么意思
  • 如何建设好政府门户网站衡阳seo排名
  • 做网站客户给不了素材推广引流渠道平台