لماذا عدد الوصول؟ كانت مدونتي الشخصية السابقة هي Bu Suanzi لزيارة الموقع ، والتي كانت جيدة جدًا ، لكن الرد كان بطيئًا لعدة مرات. ثانياً ، كانت مدونتي الشخصية قليلة جدًا من الوصول إليها ولم تكن البيانات جيدة لمشاهدة ? ...
قدم منشور المدونة السابق باختصار تكوين واستخدام Redistemplate في الربيع. تعتبر هذه المقالة حالة تطبيق بسيطة ، تعتمد بشكل أساسي على عدادات redis لتنفيذ الإحصاءات.
I. التصميم
يستخدم عداد الوصول البسيط بشكل أساسي بنية التجزئة لـ Redis ، وهيكل التخزين المقابل كما يلي:
بنية التخزين بسيطة نسبيا. من أجل التوسع ، يتوافق كل تطبيق (أو موقع) مع تطبيق ، ثم إحصائيات تراجع بناءً على مسار المسار. أخيرًا ، هناك عدد وصول خاص لتوضيح الموقع بأكمله.
الثاني. تطبيق
الشيء الرئيسي هو استخدام بنية التجزئة لـ Redis ثم تنفيذ إحصائيات البيانات. هذا ليس صعبا للغاية. يمكنك الرجوع إلى بيئة Redis في بيئة الربيع:
تكوين Redistemplate الربيع والاستخدام
1. فئة التغليف redis
بالنسبة للعديد من تلك الشائعة الاستخدام ، قمنا بإجراء تغليف بسيط ، ونحن نستخدم بشكل مباشر طريقة registemplate. بالطبع ، يمكننا أيضًا استخدام template.opsforvalue () وغيرها من الطرق المريحة. هنا نستخدم JSON لتسلسل الكائنات وتهرب من الكائنات.
الفئة العامة QuickRedisClient {Private Static Final Charset Code = charset.forname ("UTF-8") ؛ redistemplate ثابت خاص <string ، string> template ؛ سجل الفراغ الثابت العام (redistemplate <string ، string> template) {QuickRedisClient.template = template ؛ } public static void nullcheck (Object ... args) {for (object obj: args) {if (obj == null) {refl new alficalArgumentException ("redis isclument لا يمكن أن تكون فارغة!") ؛ }}} البايت الثابت العام [] tobytes (مفتاح السلسلة) {nullcheck (مفتاح) ؛ إرجاع key.getBytes (رمز) ؛ } البايت الثابت العام [] [] TOBYTES (قائمة <STRING> مفاتيح) {byte [] [] bytes = new byte [keys.size ()] [] ؛ int index = 0 ؛ لـ (مفتاح السلسلة: مفاتيح) {bytes [index ++] = tobytes (key) ؛ } إرجاع بايت ؛ } السلسلة الثابتة العامة getstr (مفتاح السلسلة) {return template.execute ((rediscallback <string>) con -> {byte [] val = con.get (tobytes (key)) ؛ return val == null؟ null: new string (val) ؛}) ؛ } Public Static void putStr (مفتاح السلسلة ، قيمة السلسلة) {template.execute ((rediscallback <Void>) con -> {con.set (tobytes (key) ، tobytes (value)) ؛ return null ؛}) ؛ } static static static long (مفتاح السلسلة ، إضافة طويلة) {return template.execute ((rediscallback <tong>) con -> {long record = con.incrby (tobytes (key) ، add) ؛ return record == null؟ 0l: record ؛}) ؛ } Hincr static static static (مفتاح السلسلة ، حقل السلسلة ، إضافة طويلة) {return template.execute ((rediscallback <tong>) con -> {long record = con.hincrby (tobytes (key) ، tobytes (field) ، add) ؛ return record == null؟ 0l:}) ؛ } static public <T> t hget (مفتاح السلسلة ، حقل السلسلة ، الفئة <T> clz) {return template.execute ((rediscallback <t>) con -> {byte [] records = con.hget (toobytes (key) ، tobytes (field)) ؛ } static public <T> map <string ، t> hmget (مفتاح السلسلة ، القائمة <string> الحقول ، الفئة <T> clz) {list <byte [] list = template.execute ((rediscallback <list <byte [] >>) con-> con.hmget (tobytes (key) ، tobytes (foableds))) ؛ if (collectionUtils.isempty (list)) {return collections.emptymap () ؛ } map <string ، t> result = new HashMap <> () ؛ لـ (int i = 0 ؛ i <fields.size () ؛ i ++) {if (list.get (i) == null) {contert ؛ } result.put (fields.get (i) ، Json.ParseObject (list.get (i) ، clz)) ؛ } نتيجة الإرجاع ؛ }} فئة التكوين المقابلة
package com.git.hui.story.cache.redis ؛ استيراد com.git.hui.story.cache.redis.serializer.defaultstrserializer ؛ استيراد org.springframework.cache.cachemanager ؛ org.springframework.context.annotation.configuration ؛ استيراد org.springframework.context.annotation.propertysource ؛ import org.springframework.core.env.envirial ؛ import org.springframework.data.cachemant. org.springframework.data.redis.connection.redisconnectionfactory ؛ استيراد org.springframework.data.redis.connection.redispassword ؛ استيراد org.springframework.data.connection.lettuce.lettuceconnection ؛ org.springframework.data.redis.core.redistemplate ؛/*** تم إنشاؤه بواسطة Yihui في 18:45 18/6/11. */@configuration@propertySource (value = "classpath: application.yml") class public redisconf {private Final Environment ؛ redisconf العامة (البيئة البيئة) {this.environment = البيئة ؛ } bean public cachemanager cachemanager () {return rediscachemanager.rediscachemanagerBuilder.fromConnectionFactory (redisconnectionfactory ()). build () ؛ } bean public rediStemplate <string ، string> redistemplate (retisconnectionfactory redisconnectionfactory) {redistemplate <string ، string> redistemplate = new redistemplate <> () ؛ redistemplate.setConnectionFactory (redisconnectionFactory) ؛ DefaultStRserializer Serializer = جديد defaultStRserializer () ؛ redistemplate.setValueserializer (Serializer) ؛ redistemplate.SethashValueserializer (Serializer) ؛ redistemplate.setKeySerializer (Serializer) ؛ redistemplate.Sethashkeyserializer (Serializer) ؛ redistemplate.afterpropertiesset () ؛ QuickRedisClient.register (Redistemplate) ؛ إرجاع redistemplate ؛ } bean public reconsisconnectionfactory redisconnectionfactory () {lettuceConnectionFactory fac = new LetTucEnconnectionFactory () ؛ FAC.GetStandalOneconFiguration (). sethostname (evًري. getProperty ("spring.redis.host")) ؛ fac.getStandalOneconFiguration (). FAC.GetStandalOneconFiguration (). setPassword (redispassword.of (explete.getProperty ("spring.redis.password"))) ؛ fac.afterpropertiesset () ؛ عودة الوجه }} 2. دعم وحدة التحكم
أولاً ، حدد معلمات الطلب:
datapublic class webCountreqDo تنفذ serializable {private appkey ؛ مرجع سلسلة خاصة ؛}والثاني هو تنفيذ واجهة وحدة التحكم. تولي القليل من الاهتمام لمنطق العد وفقًا للمسار:
@Slf4j@RestController@RequestMapping(path = "/count")public class WebCountController { @RequestMapping(path = "cc", method = {RequestMethod.GET}) public ResponseWrapper<CountDTO> addCount(WebCountReqDO webCountReqDO) { String appKey = webCountReqDO.getAppKey(); if (stringUtils.isblank (appkey)) {return responseWrapper.ErrorreturnMix (status.statusenum.illegal_params_mix ، "الرجاء تحديد appkey!") ؛ } referer = reqinfocontext.getReqInfo (). getReferer () ؛ if (stringUtils.IsBlank (enderer)) {enderer = webCounTreqDo.getReferer () ؛ } if (stringUtils.IsBlank (ender)) {return responseWrapper.ErrorreturnMix (status.statusenum.fail_mix ، "لا يمكن الحصول على طلب!") ؛ } return responseWrapper.SuccessReturn (DoupDatecnt (AppKey ، Penser)) ؛ } countdto doupdatecnt (String appkey ، string referer) {try {if (! ender.startswith ("http")) {enderer = "https: //" + enderer ؛ } uri uri = new uri (Penserer) ؛ سلسلة مضيف = uri.gethost () ؛ مسار السلسلة = uri.getPath () ؛ العد الطويل = QuickRedisClient.hincr (AppKey ، Path ، 1) ؛ TOTAL Long = QuickRedisClient.hincr (Appkey ، Host ، 1) ؛ إرجاع New Countdto (Count ، Total) ؛ } catch (استثناء e) {log.error ("Get Get Path Path Error! referer: {} ، e: {}" ، reparer ، e) ؛ إرجاع New Countdto (1L ، 1L) ؛ }}}ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.