|
|
@@ -1,15 +1,19 @@
|
|
|
package shop.alien.store.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.dao.DataIntegrityViolationException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import shop.alien.entity.result.R;
|
|
|
import shop.alien.entity.store.LifeUserPersonalizationSetting;
|
|
|
import shop.alien.mapper.LifeUserPersonalizationSettingMapper;
|
|
|
+import shop.alien.store.config.BaseRedisService;
|
|
|
import shop.alien.store.service.LifeUserPersonalizationSettingService;
|
|
|
|
|
|
@Service
|
|
|
@@ -18,6 +22,12 @@ public class LifeUserPersonalizationSettingServiceImpl
|
|
|
extends ServiceImpl<LifeUserPersonalizationSettingMapper, LifeUserPersonalizationSetting>
|
|
|
implements LifeUserPersonalizationSettingService {
|
|
|
|
|
|
+ private static final String CACHE_KEY_PREFIX = "alien:store:life:user:personalization:";
|
|
|
+ private static final long CACHE_TTL_SECONDS = 7 * 24 * 3600L;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BaseRedisService baseRedisService;
|
|
|
+
|
|
|
@Override
|
|
|
public R<String> add(LifeUserPersonalizationSetting setting) {
|
|
|
log.info("LifeUserPersonalizationSettingServiceImpl.add, param={}", setting);
|
|
|
@@ -27,6 +37,7 @@ public class LifeUserPersonalizationSettingServiceImpl
|
|
|
}
|
|
|
boolean result = this.save(setting);
|
|
|
if (result) {
|
|
|
+ invalidatePersonalizationCache(setting.getUserId());
|
|
|
return R.success("新增成功");
|
|
|
}
|
|
|
return R.fail("新增失败");
|
|
|
@@ -35,8 +46,12 @@ public class LifeUserPersonalizationSettingServiceImpl
|
|
|
@Override
|
|
|
public R<String> deleteById(Integer id) {
|
|
|
log.info("LifeUserPersonalizationSettingServiceImpl.deleteById, id={}", id);
|
|
|
+ LifeUserPersonalizationSetting old = id == null ? null : this.getById(id);
|
|
|
boolean result = this.removeById(id);
|
|
|
if (result) {
|
|
|
+ if (old != null) {
|
|
|
+ invalidatePersonalizationCache(old.getUserId());
|
|
|
+ }
|
|
|
return R.success("删除成功");
|
|
|
}
|
|
|
return R.fail("删除失败");
|
|
|
@@ -87,6 +102,8 @@ public class LifeUserPersonalizationSettingServiceImpl
|
|
|
}
|
|
|
boolean result = this.updateById(setting);
|
|
|
if (result) {
|
|
|
+ Integer uid = setting.getUserId() != null ? setting.getUserId() : existing.getUserId();
|
|
|
+ invalidatePersonalizationCache(uid);
|
|
|
return R.success("更新成功");
|
|
|
}
|
|
|
return R.fail("更新失败");
|
|
|
@@ -154,6 +171,82 @@ public class LifeUserPersonalizationSettingServiceImpl
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public LifeUserPersonalizationSetting getByUserIdCacheAside(Integer userId) {
|
|
|
+ if (userId == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String key = cacheKey(userId);
|
|
|
+ String cached = baseRedisService.getString(key);
|
|
|
+ if (StringUtils.isNotEmpty(cached)) {
|
|
|
+ try {
|
|
|
+ LifeUserPersonalizationSetting parsed = JSON.parseObject(cached, LifeUserPersonalizationSetting.class);
|
|
|
+ if (parsed != null) {
|
|
|
+ return parsed;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("LifeUserPersonalizationSetting 缓存反序列化失败,删除 key,userId={}", userId, e);
|
|
|
+ baseRedisService.delete(key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ R<LifeUserPersonalizationSetting> r = getByUserId(userId);
|
|
|
+ if (!R.isSuccess(r) || r.getData() == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ LifeUserPersonalizationSetting data = r.getData();
|
|
|
+ try {
|
|
|
+ baseRedisService.setString(key, JSON.toJSONString(data), CACHE_TTL_SECONDS);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("LifeUserPersonalizationSetting 写入缓存失败,userId={}", userId, e);
|
|
|
+ }
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean shouldSuppressLikeRelatedNotice(Integer userId) {
|
|
|
+ if (userId == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ LifeUserPersonalizationSetting s = getByUserIdCacheAside(userId);
|
|
|
+ if (s == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Integer nrm = s.getNotifyReceiveMessage();
|
|
|
+ Integer nl = s.getNotifyLike();
|
|
|
+ if (nrm != null && nrm == 0) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return nrm != null && nrm == 1 && nl != null && nl == 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean shouldSuppressFollowRelatedNotice(Integer userId) {
|
|
|
+ if (userId == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ LifeUserPersonalizationSetting s = getByUserIdCacheAside(userId);
|
|
|
+ if (s == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Integer nrm = s.getNotifyReceiveMessage();
|
|
|
+ Integer nf = s.getNotifyFollow();
|
|
|
+ if (nrm != null && nrm == 0) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return nrm != null && nrm == 1 && nf != null && nf == 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String cacheKey(Integer userId) {
|
|
|
+ return CACHE_KEY_PREFIX + userId;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void invalidatePersonalizationCache(Integer userId) {
|
|
|
+ if (userId == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ baseRedisService.delete(cacheKey(userId));
|
|
|
+ }
|
|
|
+
|
|
|
/** 与表默认值一致,便于无记录时落库 */
|
|
|
private static LifeUserPersonalizationSetting buildDefaultForUser(Integer userId) {
|
|
|
LifeUserPersonalizationSetting s = new LifeUserPersonalizationSetting();
|