Created
October 7, 2016 22:20
-
-
Save honewatson/3dddff2d28d98e94a3ee7487ba970a3d to your computer and use it in GitHub Desktop.
Moonscript Lapis cached widget examle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| lapis = require "lapis" | |
| db = require "lapis.db" | |
| html = require "lapis.html" | |
| queries = require "queries" | |
| config = require("lapis.config").get! | |
| widgets = require "views.widgets" | |
| import get_redis from require "lapis.redis" | |
| import render_html from require "lapis.html" | |
| import notNil from require "utils" | |
| query = (q, blog_id) -> | |
| return string.gsub q, '_#_', "_#{blog_id}_" | |
| get_set_redis = (key, query, view) => | |
| content = @redis\get key | |
| unless notNil content | |
| @query = query | |
| @view = view | |
| content = @view @query! | |
| @redis\set key, content | |
| return content | |
| main_query = () => | |
| @main_query_results = db.query(query(queries.main, @blog_id), 0, 40) | |
| main_posts = () => | |
| render_html -> | |
| div class: "body", -> | |
| for post in *@main_query_results | |
| div -> | |
| h2 post.post_title | |
| h3 post.post_excerpt | |
| post.post_read_more | |
| class App extends lapis.Application | |
| @before_filter => | |
| @blog_id = config.blog.default | |
| blog = db.query queries.blog, @blog_id | |
| @blog = {row.blog_option_name, row.blog_option_title for i, row in ipairs blog} | |
| @redis = get_redis! | |
| @get_set_redis = get_set_redis | |
| [index: "/"]: => | |
| content = @get_set_redis @req.parsed_url.path, main_query, main_posts | |
| { | |
| layout: false, | |
| content | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/honewatson/b29c34fe740b8e4af98053b57de64d7c