redis——Java整合

redis官網

微軟寫的windows下的redis

我們下載第一個

額案后基本一路默認就行了

安裝后,服務自動啟動,以后也不用自動啟動。

出現這個表示我們連接上了。

?

redis命令參考鏈接

Spring整合Redis

引入依賴
- spring-boot-starter-data-redis

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


配置Redis
- 配置數據庫參數

# RedisProperties
spring.redis.database=11#第11個庫,這個隨便
spring.redis.host=localhost
spring.redis.port=6379#端口


- 編寫配置類,構造RedisTemplate

這個springboot已經幫我們配了,但是默認object,我想改成string

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);// 設置key的序列化方式template.setKeySerializer(RedisSerializer.string());// 設置value的序列化方式template.setValueSerializer(RedisSerializer.json());// 設置hash的key的序列化方式template.setHashKeySerializer(RedisSerializer.string());// 設置hash的value的序列化方式template.setHashValueSerializer(RedisSerializer.json());template.afterPropertiesSet();return template;}}


訪問Redis
- redisTemplate.opsForValue()
- redisTemplate.opsForHash()
- redisTemplate.opsForList()
- redisTemplate.opsForSet()
- redisTemplate.opsForZSet()

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class RedisTests {@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void testStrings() {String redisKey = "test:count";redisTemplate.opsForValue().set(redisKey, 1);System.out.println(redisTemplate.opsForValue().get(redisKey));System.out.println(redisTemplate.opsForValue().increment(redisKey));System.out.println(redisTemplate.opsForValue().decrement(redisKey));}@Testpublic void testHashes() {String redisKey = "test:user";redisTemplate.opsForHash().put(redisKey, "id", 1);redisTemplate.opsForHash().put(redisKey, "username", "zhangsan");System.out.println(redisTemplate.opsForHash().get(redisKey, "id"));System.out.println(redisTemplate.opsForHash().get(redisKey, "username"));}@Testpublic void testLists() {String redisKey = "test:ids";redisTemplate.opsForList().leftPush(redisKey, 101);redisTemplate.opsForList().leftPush(redisKey, 102);redisTemplate.opsForList().leftPush(redisKey, 103);System.out.println(redisTemplate.opsForList().size(redisKey));System.out.println(redisTemplate.opsForList().index(redisKey, 0));System.out.println(redisTemplate.opsForList().range(redisKey, 0, 2));System.out.println(redisTemplate.opsForList().leftPop(redisKey));System.out.println(redisTemplate.opsForList().leftPop(redisKey));System.out.println(redisTemplate.opsForList().leftPop(redisKey));}@Testpublic void testSets() {String redisKey = "test:teachers";redisTemplate.opsForSet().add(redisKey, "劉備", "關羽", "張飛", "趙云", "諸葛亮");System.out.println(redisTemplate.opsForSet().size(redisKey));System.out.println(redisTemplate.opsForSet().pop(redisKey));System.out.println(redisTemplate.opsForSet().members(redisKey));}@Testpublic void testSortedSets() {String redisKey = "test:students";redisTemplate.opsForZSet().add(redisKey, "唐僧", 80);redisTemplate.opsForZSet().add(redisKey, "悟空", 90);redisTemplate.opsForZSet().add(redisKey, "八戒", 50);redisTemplate.opsForZSet().add(redisKey, "沙僧", 70);redisTemplate.opsForZSet().add(redisKey, "白龍馬", 60);System.out.println(redisTemplate.opsForZSet().zCard(redisKey));System.out.println(redisTemplate.opsForZSet().score(redisKey, "八戒"));System.out.println(redisTemplate.opsForZSet().reverseRank(redisKey, "八戒"));System.out.println(redisTemplate.opsForZSet().reverseRange(redisKey, 0, 2));}@Testpublic void testKeys() {redisTemplate.delete("test:user");System.out.println(redisTemplate.hasKey("test:user"));redisTemplate.expire("test:students", 10, TimeUnit.SECONDS);}
}

這樣還是稍微有點麻煩,我們其實可以綁定key

    // 多次訪問同一個key@Testpublic void testBoundOperations() {String redisKey = "test:count";BoundValueOperations operations = redisTemplate.boundValueOps(redisKey);operations.increment();operations.increment();operations.increment();operations.increment();operations.increment();System.out.println(operations.get());}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/445250.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/445250.shtml
英文地址,請注明出處:http://en.pswp.cn/news/445250.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

無限踩坑系列(4)-遠程登入服務器

遠程操作服務器1.遠程上傳/下載命令&#xff08;文件夾/文件&#xff09;2.文本編輯vim3.一直保持服務器登入狀態4.虛擬終端screenssh遠程登入服務器&#xff0c;沒有圖形界面&#xff0c;只能在終端中操作文件與文件夾。本文總結了遠程登入服務器過程中用到的一些命令。1.遠程…

程序員不成熟的若干個特征

做我們這個項目也是一樣&#xff0c;很多人來做這個生意&#xff0c;開始沒有什么成績&#xff0c;就想著要放棄&#xff0c;有的人一個月放棄&#xff0c;有的人三個月放棄&#xff0c;有的人半年放棄&#xff0c;有的人一年放 棄&#xff0c;我不明白人們為什么輕易放棄這個趨…

一文理解KMP算法

一文理解KMP算法 作者&#xff1a;July 時間&#xff1a;最初寫于2011年12月&#xff0c;2014年7月21日晚10點 全部刪除重寫成此文&#xff0c;隨后的半個多月不斷反復改進。后收錄于新書《編程之法&#xff1a;面試和算法心得》第4.4節中。 1. 引言 本KMP原文最初寫于2年多前的…

小貓的java基礎知識點匯總(下)

1、線程和進程有什么區別&#xff1f; 進程是操作系統資源分配的基本單位&#xff0c;而線程是任務調度和執行的基本單位 線程是進程的子集&#xff0c;一個進程可以有很多線程&#xff0c;每條線程并行執行不同的任務。 不同的進程使用不同的內存空間&#xff0c;而所有的線…

無數踩坑系列(3)-配置pytorch

配置pytorch環境1. 命令一鍵式安裝2.源碼安裝問題1問題2問題3問題43.克隆一個已有環境&#xff0c;帶pytorch4.GPU驅動版本不對在實際開發中&#xff0c;想要在自己的機子上跑別人的代碼&#xff1b;或者&#xff0c;在新的機子上跑自己的代碼&#xff0c;總是面臨著環境配置的…

小貓的java基礎知識點匯總(上)

1、一個".java"源文件中是否可以包括多個類&#xff08;不是內部類&#xff09;&#xff1f;有什么限制&#xff1f; 可以有多個類&#xff0c;但只能有一個public的類&#xff0c;并且public的類名必須與文件名相一致。 2、short s1 1; s1 s11; 有沒有錯&#xff…

機器學習算法分類總結

機器學習方法分類總結 這篇文章只是一個類似于知識概括的文章&#xff0c;主要作用是幫忙梳理&#xff1a; 1) 分類 貝葉斯模型&#xff08;Bayesian Mode&#xff09; - 樸素貝葉斯算法&#xff08;Naive Bayesian Mode&#xff09; - 平均單依賴估計&#xff08;AveragedO…

無限踩坑系列(5)-MySQLdb

MySQLdb在Python2.x 時使用的是MySQLdbpython3中這個庫已經不再使用了&#xff0c;所有的功能都由pymysql或mysqlclient替代。所以 想在python3中配MySQLdb真是一個深的不能再深的坑了。下面記錄了愚蠢的填坑過程&#xff0c;僅做有類似錯誤的參考。參考文檔&#xff1a;https:…

后端 分頁組件實例

/*** 分頁相關信息*/ public class Page {//當前頁碼private int current1;//顯示的上限private int limit10;//數據總數//用于計算頁數private int rows;//路徑private String path;public int getCurrent() {return current;}public void setCurrent(int current) {if (curre…

大數據學習(07)--MapReduce

文章目錄目錄1.MapReduce介紹1.1 什么是分布式并行編程&#xff1f;1.2 MapReduce模型介紹1.3 map和reduce函數2.MapReduce體系架構3.MapReduce工作流程3.1 概述3.2 MapReduce各個階段介紹3.3 shuffle過程介紹3.3.1 shuffle過程簡介3.3.2 map中的shuffle過程3.3.3 reduce中的sh…

關閉用playsound函數的WAV文件

播放聲音文件 PlaySound函數應用 1.關閉用playsound函數的WAV文件 PlaySound(0,NULL,0);即可 // test2.cpp : Defines the entry point for the application.//#include "stdafx.h"#include <mmsystem.h>int APIENTRY WinMain(HINSTANCE hInstance, …

身份驗證

傳統身份驗證的方法 HTTP 是一種沒有狀態的協議&#xff0c;也就是它并不知道是誰是訪問應用。這里我們把用戶看成是客戶端&#xff0c;客戶端使用用戶名還有密碼通過了身份驗證&#xff0c;不過下回這個客戶端再發送請求時候&#xff0c;還得再驗證一下。 解決的方法就是&…

Pytorch(4)-模型保存-載入-eval()

模型保存與提取1. 整個模型 保存-載入2. 僅模型參數 保存-載入3. GPU/CPU模型保存與導入4. net.eval()--固定模型隨機項神經網絡模型在線訓練完之后需要保存下來&#xff0c;以便下次使用時可以直接導入已經訓練好的模型。pytorch 提供兩種方式保存模型:方式1&#xff1a;保存整…

大數據學習(08)--Hadoop中的數據倉庫Hive

文章目錄目錄1.什么是數據倉庫&#xff1f;1.1數據倉庫概念1.2傳統數據倉庫面臨的挑戰1.3 Hive介紹1.4 Hive與傳統數據庫的對比1.5 Hive在企業中的部署與應用2.Hive系統架構3.Hive工作原理3.1 SQL轉換為MapReduce作業的基本原理3.2 Hive中SQL查詢轉換MapReduce作業的過程4.Hive…

dubbo知識點總結 持續更新

Dubbo 支持哪些協議&#xff0c;每種協議的應用場景&#xff0c;優缺點&#xff1f; ? dubbo&#xff1a; 單一長連接和 NIO 異步通訊&#xff0c;適合大并發小數據量的服務調用&#xff0c; 以及消費者遠大于提供者。傳輸協議 TCP&#xff0c;異步&#xff0c;Hessian 序列化…

使用Linux auto Makefile自動生成的運行步驟

首先創建一個 Linux Makefile.am.這一步是創建Linux Makefile很重要的一步&#xff0c;automake要用的腳本配置文件是Linux Makefile.am&#xff0c;用戶需要自己創建相應的文件。之后&#xff0c;automake工具轉換成Linux Makefile.in。AD&#xff1a; 在向大家詳細介紹Linux …

無限踩坑系列(6)-mySQL數據庫鏈接錯誤

mySQL數據庫鏈接錯誤錯誤1錯誤2長鏈接短連接應用場景需要一直訪問mySQL數據庫&#xff0c;遇到如下錯誤&#xff1a;錯誤1 釋放已經釋放的數據庫鏈接conn.&#xff0c;或者&#xff0c;操作已經釋放的數據庫鏈接conn.或者失去鏈接后再操作數據庫都可能會報這個錯誤 aise err.I…

初探函數式編程和面對對象式編程

文章目錄目錄1.函數式編程和面向對象編程概念1.1 函數式編程1.2 面向對象編程2.函數式編程和面向對象編程的優缺點2.1 函數式編程優點缺點2.2 面對對象編程優點缺點3.為什么在并行計算中函數式編程比較好3.1 什么是并行計算3.2 函數式編程興起原因目錄 1.函數式編程和面向對象…

linux常用解壓和壓縮文件的命令

linux常用解壓和壓縮文件的命令 .tar 解包&#xff1a;tar xvf FileName.tar打包&#xff1a;tar cvf FileName.tar DirName&#xff08;注&#xff1a;tar是打包&#xff0c;不是壓縮&#xff01;&#xff09;———————————————.gz解壓1&#xff1a;gunzip FileN…

Python外(4)-讀寫mat文件

讀寫mat文件1.讀取2.寫入.mat 是matlab中數據存儲的標準格式&#xff0c;Python中能夠通過庫scipy讀取和保存。導入scipy庫 from scipy import io 1.讀取 io.loadmat(file_name, mdictNone, appendmatTrue, **kwargs) 簡便方式&#xff1a; io.loadmat(file_name) append mat–…