Skip to content

Commit 0db6a28

Browse files
committed
取消全站缓存.完善缓存功能
1 parent 9719c27 commit 0db6a28

File tree

10 files changed

+442
-26
lines changed

10 files changed

+442
-26
lines changed

DjangoBlog/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151
'django.middleware.security.SecurityMiddleware',
5252
'django.contrib.sessions.middleware.SessionMiddleware',
5353
'django.middleware.gzip.GZipMiddleware',
54-
'django.middleware.cache.UpdateCacheMiddleware',
54+
#'django.middleware.cache.UpdateCacheMiddleware',
5555
'django.middleware.common.CommonMiddleware',
56-
'django.middleware.cache.FetchFromCacheMiddleware',
56+
#'django.middleware.cache.FetchFromCacheMiddleware',
5757
'django.middleware.csrf.CsrfViewMiddleware',
5858
'django.contrib.auth.middleware.AuthenticationMiddleware',
5959
'django.contrib.messages.middleware.MessageMiddleware',

DjangoBlog/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ def get_max_articleid_commentid():
3333
return (Article.objects.latest().pk, Comment.objects.latest().pk)
3434

3535

36+
def get_md5(str):
37+
m = md5(str.encode('utf-8'))
38+
return m.hexdigest()
39+
40+
3641
def cache_decorator(expiration=3 * 60):
3742
def wrapper(func):
3843
def news(*args, **kwargs):

blog/models.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from uuslug import slugify
55
from DjangoBlog.spider_notify import spider_notify
66
from django.contrib.sites.models import Site
7-
from DjangoBlog.utils import cache_decorator, logger
7+
from DjangoBlog.utils import cache_decorator, logger, cache
88
from django.utils.functional import cached_property
99

1010

@@ -114,11 +114,17 @@ def viewed(self):
114114
self.views += 1
115115
self.save(update_fields=['views'])
116116

117-
"""
118117
def comment_list(self):
119-
comments = self.comment_set.all()
120-
parent_comments = comments.filter(parent_comment=None)
121-
"""
118+
cache_key = 'article_comments_{id}'.format(id=self.id)
119+
value = cache.get(cache_key)
120+
if value:
121+
logger.info('get article comments:{id}'.format(id=self.id))
122+
return value
123+
else:
124+
comments = self.comment_set.all()
125+
cache.set(cache_key, comments)
126+
logger.info('set article comments:{id}'.format(id=self.id))
127+
return comments
122128

123129
def get_admin_url(self):
124130
info = (self._meta.app_label, self._meta.model_name)

blog/static/blog/css/style.css

Lines changed: 306 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ article.sticky .featured-post {
923923
font-size: 12px;
924924
font-size: 0.857142857rem;
925925
line-height: 2;
926-
background-color: rgba(0,0,0,0.04);
926+
background-color: rgba(0, 0, 0, 0.04);
927927
}
928928

929929
.entry-content pre,
@@ -2358,3 +2358,308 @@ li #reply-title {
23582358
font: inherit;
23592359
vertical-align: baseline;
23602360
}
2361+
2362+
.icon-sn-google {
2363+
background-position: 0 -28px;
2364+
}
2365+
2366+
.icon-sn-bg-google {
2367+
background-color: #4285f4;
2368+
background-position: 0 0;
2369+
}
2370+
2371+
.fa-sn-google {
2372+
color: #4285f4;
2373+
}
2374+
2375+
.icon-sn-github {
2376+
background-position: -28px -28px;
2377+
}
2378+
2379+
.icon-sn-bg-github {
2380+
background-color: #333;
2381+
background-position: -28px 0;
2382+
}
2383+
2384+
.fa-sn-github {
2385+
color: #333;
2386+
}
2387+
2388+
.icon-sn-weibo {
2389+
background-position: -56px -28px;
2390+
}
2391+
2392+
.icon-sn-bg-weibo {
2393+
background-color: #e90d24;
2394+
background-position: -56px 0;
2395+
}
2396+
2397+
.fa-sn-weibo {
2398+
color: #e90d24;
2399+
}
2400+
2401+
.icon-sn-qq {
2402+
background-position: -84px -28px;
2403+
}
2404+
2405+
.icon-sn-bg-qq {
2406+
background-color: #0098e6;
2407+
background-position: -84px 0;
2408+
}
2409+
2410+
.fa-sn-qq {
2411+
color: #0098e6;
2412+
}
2413+
2414+
.icon-sn-twitter {
2415+
background-position: -112px -28px;
2416+
}
2417+
2418+
.icon-sn-bg-twitter {
2419+
background-color: #50abf1;
2420+
background-position: -112px 0;
2421+
}
2422+
2423+
.fa-sn-twitter {
2424+
color: #50abf1;
2425+
}
2426+
2427+
.icon-sn-facebook {
2428+
background-position: -140px -28px;
2429+
}
2430+
2431+
.icon-sn-bg-facebook {
2432+
background-color: #4862a3;
2433+
background-position: -140px 0;
2434+
}
2435+
2436+
.fa-sn-facebook {
2437+
color: #4862a3;
2438+
}
2439+
2440+
.icon-sn-renren {
2441+
background-position: -168px -28px;
2442+
}
2443+
2444+
.icon-sn-bg-renren {
2445+
background-color: #197bc8;
2446+
background-position: -168px 0;
2447+
}
2448+
2449+
.fa-sn-renren {
2450+
color: #197bc8;
2451+
}
2452+
2453+
.icon-sn-tqq {
2454+
background-position: -196px -28px;
2455+
}
2456+
2457+
.icon-sn-bg-tqq {
2458+
background-color: #1f9ed2;
2459+
background-position: -196px 0;
2460+
}
2461+
2462+
.fa-sn-tqq {
2463+
color: #1f9ed2;
2464+
}
2465+
2466+
.icon-sn-douban {
2467+
background-position: -224px -28px;
2468+
}
2469+
2470+
.icon-sn-bg-douban {
2471+
background-color: #279738;
2472+
background-position: -224px 0;
2473+
}
2474+
2475+
.fa-sn-douban {
2476+
color: #279738;
2477+
}
2478+
2479+
.icon-sn-weixin {
2480+
background-position: -252px -28px;
2481+
}
2482+
2483+
.icon-sn-bg-weixin {
2484+
background-color: #00b500;
2485+
background-position: -252px 0;
2486+
}
2487+
2488+
.fa-sn-weixin {
2489+
color: #00b500;
2490+
}
2491+
2492+
.icon-sn-dotted {
2493+
background-position: -280px -28px;
2494+
}
2495+
2496+
.icon-sn-bg-dotted {
2497+
background-color: #eee;
2498+
background-position: -280px 0;
2499+
}
2500+
2501+
.fa-sn-dotted {
2502+
color: #eee;
2503+
}
2504+
2505+
.icon-sn-site {
2506+
background-position: -308px -28px;
2507+
}
2508+
2509+
.icon-sn-bg-site {
2510+
background-color: #00b500;
2511+
background-position: -308px 0;
2512+
}
2513+
2514+
.fa-sn-site {
2515+
color: #00b500;
2516+
}
2517+
2518+
.icon-sn-linkedin {
2519+
background-position: -336px -28px;
2520+
}
2521+
2522+
.icon-sn-bg-linkedin {
2523+
background-color: #0077b9;
2524+
background-position: -336px 0;
2525+
}
2526+
2527+
.fa-sn-linkedin {
2528+
color: #0077b9;
2529+
}
2530+
2531+
[class*=icon-sn-] {
2532+
display: inline-block;
2533+
background-image: url('../img/icon-sn.svg');
2534+
background-repeat: no-repeat;
2535+
width: 28px;
2536+
height: 28px;
2537+
vertical-align: middle;
2538+
background-size: auto 56px;
2539+
}
2540+
2541+
[class*=icon-sn-]:hover {
2542+
opacity: .8;
2543+
filter: alpha(opacity=80);
2544+
}
2545+
2546+
.btn-sn-google {
2547+
background: #4285f4;
2548+
}
2549+
2550+
.btn-sn-google:active, .btn-sn-google:focus, .btn-sn-google:hover {
2551+
background: #2a75f3;
2552+
}
2553+
2554+
.btn-sn-github {
2555+
background: #333;
2556+
}
2557+
2558+
.btn-sn-github:active, .btn-sn-github:focus, .btn-sn-github:hover {
2559+
background: #262626;
2560+
}
2561+
2562+
.btn-sn-weibo {
2563+
background: #e90d24;
2564+
}
2565+
2566+
.btn-sn-weibo:active, .btn-sn-weibo:focus, .btn-sn-weibo:hover {
2567+
background: #d10c20;
2568+
}
2569+
2570+
.btn-sn-qq {
2571+
background: #0098e6;
2572+
}
2573+
2574+
.btn-sn-qq:active, .btn-sn-qq:focus, .btn-sn-qq:hover {
2575+
background: #0087cd;
2576+
}
2577+
2578+
.btn-sn-twitter {
2579+
background: #50abf1;
2580+
}
2581+
2582+
.btn-sn-twitter:active, .btn-sn-twitter:focus, .btn-sn-twitter:hover {
2583+
background: #38a0ef;
2584+
}
2585+
2586+
.btn-sn-facebook {
2587+
background: #4862a3;
2588+
}
2589+
2590+
.btn-sn-facebook:active, .btn-sn-facebook:focus, .btn-sn-facebook:hover {
2591+
background: #405791;
2592+
}
2593+
2594+
.btn-sn-renren {
2595+
background: #197bc8;
2596+
}
2597+
2598+
.btn-sn-renren:active, .btn-sn-renren:focus, .btn-sn-renren:hover {
2599+
background: #166db1;
2600+
}
2601+
2602+
.btn-sn-tqq {
2603+
background: #1f9ed2;
2604+
}
2605+
2606+
.btn-sn-tqq:active, .btn-sn-tqq:focus, .btn-sn-tqq:hover {
2607+
background: #1c8dbc;
2608+
}
2609+
2610+
.btn-sn-douban {
2611+
background: #279738;
2612+
}
2613+
2614+
.btn-sn-douban:active, .btn-sn-douban:focus, .btn-sn-douban:hover {
2615+
background: #228330;
2616+
}
2617+
2618+
.btn-sn-weixin {
2619+
background: #00b500;
2620+
}
2621+
2622+
.btn-sn-weixin:active, .btn-sn-weixin:focus, .btn-sn-weixin:hover {
2623+
background: #009c00;
2624+
}
2625+
2626+
.btn-sn-dotted {
2627+
background: #eee;
2628+
}
2629+
2630+
.btn-sn-dotted:active, .btn-sn-dotted:focus, .btn-sn-dotted:hover {
2631+
background: #e1e1e1;
2632+
}
2633+
2634+
.btn-sn-site {
2635+
background: #00b500;
2636+
}
2637+
2638+
.btn-sn-site:active, .btn-sn-site:focus, .btn-sn-site:hover {
2639+
background: #009c00;
2640+
}
2641+
2642+
.btn-sn-linkedin {
2643+
background: #0077b9;
2644+
}
2645+
2646+
.btn-sn-linkedin:active, .btn-sn-linkedin:focus, .btn-sn-linkedin:hover {
2647+
background: #0067a0;
2648+
}
2649+
2650+
[class*=btn-sn-], [class*=btn-sn-]:active, [class*=btn-sn-]:focus, [class*=btn-sn-]:hover {
2651+
border: none;
2652+
color: #fff;
2653+
}
2654+
2655+
.btn-sn-more {
2656+
padding: 0;
2657+
}
2658+
2659+
.btn-sn-more, .btn-sn-more:active, .btn-sn-more:hover {
2660+
box-shadow: none;
2661+
}
2662+
2663+
[class*=btn-sn-] [class*=icon-sn-] {
2664+
background-color: transparent;
2665+
}

blog/static/blog/img/icon-sn.svg

Lines changed: 1 addition & 0 deletions
Loading

blog/urls.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@
2020
from haystack.query import SearchQuerySet
2121
from haystack.views import SearchView
2222

23-
24-
def key_prefixer(request):
25-
# if it's not there, don't cache
26-
# return request.GET.get('number')
27-
return "123"
28-
29-
3023
urlpatterns = [
3124
url(r'^$', views.IndexView.as_view(), name='index'),
3225
url(r'^page/(?P<page>\d+)$', views.IndexView.as_view(), name='index_page'),

0 commit comments

Comments
 (0)