Skip to main content

Ecache for sqlalchemy

Run test

make unittest

Installation / Rquirements

pip intall ecache

Usage

With Flask Integrate

from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy

from ecache.ext.flask_cache import CacheableMixin, query_callable, regions

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
app.debug = True
db = SQLAlchemy(app)

class User(db.Model, CacheableMixin):
    """Default backend is redis and expiration time is 1 hour, default
    region name is `default`, you can override this:

        cache_regions = your_regions
        cache_label = your_label
    """

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)

@app.route('/users')
def all_users():
    """Result will try to get from cache first. load from db if cache miss.
    """
    users = [user.to_dict() for user in User.cache.filter()]
    return jsonify(users=users)


@app.route('/users/<int:user_id')
def view_user(user_id):
    """Result will try to get from cache first. load from db if cache miss.
    """
    user = User.cache.get(user_id)
    return jsonify(user.to_dict())

More detail see example

With Pure SQLAlchemy model Integrate

# -*- coding: utf-8 -*-

import redis
from sqlalchemy import (Column, Integer, String, SmallInteger)

from ecache.core import cache_mixin
from ecache.db import db_manager, model_base

# alsosee :class:`ecache.db.DBManager`
DBSession = db_manager.get_session('test')
cache_client = redis.StrictRedis()
CacheMixin = cache_mixin()
DeclarativeBase = model_base()


class TodoListModel(DeclarativeBase, CacheMixin):
    __tablename__ == 'todo_list'
    TABLE_CACHE_EXPIRATION_TIME = 3600

    id = Column(Integer, primary_key=True)
    title = Column(String, default='')
    is_done = Column(SmallInteger, default=0)

    @classmethod
    def get_todo(cls, todo_id):
        todo = cls.get(todo_id)  # `cls.get` inherited from `CacheMixin`
        return todo

    @classmethod
    def add(cls, title):
        todo = cls(title=title)
        s = DBSession()
        s.add(todo)
        s.commit()

Metadata

Release files for ecache 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ecache 0.1.2
File Size Uploaded Provenance
ecache-0.1.2.tar.gz 11.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for ecache 0.1.2
File Interpreter ABI Platform
ecache-0.1.2-py2-none-any.whl Python 2 none any Details

Total release size: 28.6 kB

Release files / ecache-0.1.2.tar.gz

Download URL ecache-0.1.2.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
31c7fb9a2827a8609a8c25b4b8decdcf837726b276b1aaed4b69ea09546e2304
BLAKE2b-256 checksum
How to use checksums
ad6ec62aa9904b2fad1247b14c7f3b8ce07555ccb6b567a4017874c48a014a23
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / ecache-0.1.2-py2-none-any.whl

Download URL ecache-0.1.2-py2-none-any.whl
Size 16.7 kB
Tags Python 2
SHA-256 checksum
How to use checksums
b6775d27993086110427a72bc7ffe94ecd17ae4b71fbef52ef3951e5d2b7988e
BLAKE2b-256 checksum
How to use checksums
ef39c84485775a4dbab646bddb0071f90389549157323aeb6862c978c48c30bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for ecache 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page